HTTPWebRequest and "X_Session"? - c#

I am new to interacting with a webpage in C#. I have come across a problem where a server returns a 401 unauthorized access exception, even though I provided correct credentials. I have traced the source of my problem to something called an "X_Session" as in below:
How would I generate one of these?
The code I currently have is:
private void snd_Click(object sender, EventArgs e)
{
string pnum = number.Text;
string msg = text.Text;
StringBuilder postData = new StringBuilder();
postData.Append("%7B%22contact_value%22%3A%22" + pnum + "%22%2C");
postData.Append("%22contact_type%22%3A2%2C");
postData.Append("%22message%22%3A%22" + msg + "%22%2C");
postData.Append("%22read%22%3A1%2C");
postData.Append("%22message_direction%22%3A2%2C");
postData.Append("%22message_type%22%3A1%2C");
postData.Append("%22date%22%3A%22Sat+Nov+30+2013+13%3A20%3A44+GMT-0800+(Pacific+Standard+Time)%22%2C");
postData.Append("%22from_name%22%3A%22[redacted name]%22%7D");
UTF8Encoding encoding = new UTF8Encoding();
byte[] byData = encoding.GetBytes(postData.ToString());
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("https://www.textnow.com/api/users/[redacted username]/messages"));
WebResponse response = null;
req.ProtocolVersion = HttpVersion.Version11;
System.Net.NetworkCredential netCredential =
new System.Net.NetworkCredential("[redacted username]", "[redacted password]");
req.Credentials = netCredential;
req.PreAuthenticate = true;
req.KeepAlive = true;
req.Method = "OPTIONS";
req.Host = "www.textnow.com";
req.Accept = "*/*";
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
req.Headers.Add("Access-Control-Request-Headers","accept, origin, x_session, content-type");
req.Headers.Add("Access-Control-Request-Method","POST");
response = (HttpWebResponse)req.GetResponse();
HttpWebRequest mreq = (HttpWebRequest)WebRequest.Create(new Uri("https://www.textnow.com/api/users/csharpautomaton/messages"));
mreq.Method = "POST";
mreq.Host = "www.textnow.com";
mreq.ProtocolVersion = HttpVersion.Version11;
mreq.Accept = "application/json, text/javascript, */*; q=0.01";
mreq.Headers.Add("Accept-Encoding","gzip,deflate,sdch");
mreq.Headers.Add("Accept-Language", "en-US,en;q=0.8");
mreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
mreq.Credentials = netCredential;
mreq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
mreq.Referer = "https://www.textnow.com/api/users/[redacted username]/messages";
mreq.AllowAutoRedirect = true;
mreq.KeepAlive = true;
mreq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
Stream sw = mreq.GetRequestStream();
sw.Write(byData,0,byData.Length);
sw.Close();
response = (HttpWebResponse)mreq.GetResponse();
}

Who created the X_session value in the sample you posted?
Whoever created that value will know what the rules are for creating it.
It was probably setup on some prior request and saved for later use.

Related

Multi-webrequests & WebClient

I'm trying to the html information on a certain a website so I can parse out the info for our database. The problem is that the second & third responseFromServer info comes back the same. However, when I follow the links on inside a real web browser, I get the right information (correct page).
I'm thinking that each WebRequest is basically opening a 'new' instance of the web and what I want it to do is use the same instance.
Is there a way to specify (using a WebClient?) so that each request is contained in the 'same browser' (for lack of a better term)
static void CountyInfo(string Address)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Cache-Control: private");
webClient.Headers.Add("Content-Type: text/html; charset=utf-8");
webClient.Headers.Add("Server: Microsoft-IIS/6.0");
webClient.Headers.Add("X-AspNet-Version: 4.0.30319");
webClient.Headers.Add("X-Powered-By: ASP.NET");
webClient.Headers.Add("X-UA-Compatible: IE=8, IE=9, IE=10, IE=11");
Address = Address.Replace(" ", "+");
string url1 = "http://mcassessor.maricopa.gov/?s=" + Address;
WebRequest request1 = WebRequest.Create(url1);
WebResponse response1 = request1.GetResponse();
//Stream dataStream1 = response1.GetResponseStream();
Stream dataStream1 = webClient.OpenRead(url1);
StreamReader reader1 = new StreamReader(dataStream1);
string responseFromServer1 = reader1.ReadToEnd();
string ParcelNum = getBetween(responseFromServer1, "http://treasurer.maricopa.gov/parcels/default.asp?Parcel=", "target=");
ParcelNum = new String(ParcelNum.Where(Char.IsDigit).ToArray());
//reader1.Close();
//response1.Close();
//NEW GET request
string url2 = "http://treasurer.maricopa.gov/parcels/default.asp?Parcel=" + ParcelNum;
WebRequest request2 = WebRequest.Create(url2);
WebResponse response2 = request2.GetResponse();
//Stream dataStream2 = response2.GetResponseStream();
Stream dataStream2 = webClient.OpenRead(url2);
StreamReader reader2 = new StreamReader(dataStream2);
string responseFromServer2 = reader2.ReadToEnd();
//reader2.Close();
//response2.Close();
//NEW GET request
string url3 = "http://treasurer.maricopa.gov/Parcel/" + "TaxDetails.aspx?taxyear=2013";
WebRequest request3 = WebRequest.Create(url3);
WebResponse response3 = request3.GetResponse();
//Stream dataStream3 = response3.GetResponseStream();
Stream dataStream3 = webClient.OpenRead(url3);
StreamReader reader3 = new StreamReader(dataStream3);
string responseFromServer3 = reader3.ReadToEnd();
reader3.Close();
response3.Close();
}
EDIT: just saw this. request1 gives me the correct page (the query results page) but request 2 and 3 return me back to the "Home Page" of the website. Even though i am passing in url2 and url3 into the requests2 & 3 respectively.
You need to manage the cookies. Get the cookies served by the web server and pass them back on the next request.
Sorry about the long code post, but this is how i got it working. Let me know if you can condense it any.
static public CookieContainer cookieJar;
static void Main(string[] args)
{
string ControlNumber = "######";
GetOrderInfo newOrder = new GetOrderInfo(ControlNumber);
obtainCookies();
MARICOPAcounty(newOrder.OrderAddress);
}
static void obtainCookies()
{
string postData = "http://mcassessor.maricopa.gov/";
CookieContainer tempCookies = new CookieContainer();
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(postData);
HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://mcassessor.maricopa.gov/");
postReq.Method = "POST";
postReq.KeepAlive = true;
Cookie chocoChip = new Cookie("_ga", "GA1.2.1813386723.1386802842") { Domain = "http://mcassessor.maricopa.gov/" };
postReq.CookieContainer = new CookieContainer();
postReq.CookieContainer.Add(chocoChip);
postReq.ContentType = "text/html; charset=utf-8";
postReq.Referer = "http://mcassessor.maricopa.gov/";
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MAM3; rv:11.0) like Gecko";
postReq.ContentLength = byteData.Length;
Stream postReqStream = postReq.GetRequestStream();
postReqStream.Write(byteData, 0, byteData.Length);
postReqStream.Close();
HttpWebResponse postResponse;
postResponse = (HttpWebResponse)postReq.GetResponse();
postResponse.Cookies.Add(chocoChip);
StreamReader postReqReader = new StreamReader(postResponse.GetResponseStream());
tempCookies.Add(chocoChip);
cookieJar = tempCookies;
string soureCode = postReqReader.ReadToEnd();
if (postReq != null)
{
Console.WriteLine("\r\n\r\n postResponse COOKIES");
Console.WriteLine(postResponse.Cookies[0]);
Console.WriteLine("\r\n\r\n postReq Headers");
Console.WriteLine(postReq.Headers.ToString());
Console.WriteLine("\r\n\r\n postResponse Headers");
Console.Write("\t" + postResponse.Headers);
}
}
public static string MARICOPAcounty(string Address)
{
//-------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------------------//
Address = Address.Replace(" ", "+");
string url1 = "http://mcassessor.maricopa.gov/?s=" + Address;
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
request1.CookieContainer = cookieJar;
request1.Method = "GET";
request1.Accept = "text/html, application/xhtml+xml, */*";
request1.Headers.Add("Accept-Language: en-US");
request1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MAM3; rv:11.0) like Gecko";
request1.Headers.Add("Accept-Enconding: gzip, deflate");
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
StreamReader sr1 = new StreamReader(response1.GetResponseStream());
string sourceCode1 = sr1.ReadToEnd();
string ParcelNum = getBetween(sourceCode1, "http://treasurer.maricopa.gov/parcels/default.asp?Parcel=", "target=");
ParcelNum = new String(ParcelNum.Where(Char.IsDigit).ToArray());
if (response1 != null)
{
Console.WriteLine("\r\n\r\n request1 Headers");
Console.WriteLine(request1.Headers.ToString());
Console.WriteLine("\r\n\r\nresponse1 Headers");
Console.Write("\t" + response1.Headers);
}
sr1.Close();
response1.Close();
//-------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------------------//
//NEW GET request
string url2 = "http://treasurer.maricopa.gov/parcels/default.asp?Parcel=50423370"; //+ ParcelNum;
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2);
request2.CookieContainer = cookieJar;
request2.Method = "GET";
request2.Accept = "text/html, application/xhtml+xml, */*";
request2.Headers.Add("Accept-Language: en-US");
request2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MAM3; rv:11.0) like Gecko";
request2.Headers.Add("Accept-Encoding: gzip, deflate");
request2.Referer = url1;
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream());
string sourceCode2 = sr2.ReadToEnd();
if (response2 != null)
{
Console.WriteLine("\r\n\r\nrrequest2 Headers");
Console.WriteLine(request2.Headers);
Console.WriteLine(request2.CookieContainer);
Console.WriteLine("\r\n\r\nresponse2 Headers");
Console.Write("\t" + response2.Headers);
}
sr2.Close();
response2.Close();
//-------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------------------//
//new GET request
string url3 = "http://treasurer.maricopa.gov/Parcel/TaxDetails.aspx?taxyear=2013";
HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(url3);
request3.CookieContainer = cookieJar;
request3.Method = "GET";
request3.Accept = "text/html, application/xhtml+xml, */*";
request3.Headers.Add("Accept-Language: en-US");
request3.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; MAM3; rv:11.0) like Gecko";
request3.Headers.Add("Accept-Encoding: gzip, deflate");
request3.Referer = url2;
HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
StreamReader sr3 = new StreamReader(response3.GetResponseStream());
string sourceCode3 = sr3.ReadToEnd();
if (response3 != null)
{
Console.WriteLine("\r\n\r\nrrequest3 Headers");
Console.WriteLine(request3.Headers);
Console.WriteLine("\r\n\r\nresponse3 Headers");
Console.Write("\t" + response3.Headers);
}
sr3.Close();
response3.Close();
return sourceCode3;
}

How to use c# login wordpress

This example login function But does not work for loading the Dashboard page wordpress.
I need to perform some action in wordpress admin panel programmatically but can't manage how to login to Wordpress using C# and HttpWebRequest.
Here is what I do:
namespace Wordpress_Login
{
public class WPWorkerTests
{
CookieContainer cookieJar = new CookieContainer();
public String StartAdmin()
{
//string wp_url = "http://localhost/wp";
string admin_url = "http://localhost/wp/wp-admin";
string login_url = "http://localhost/wp/wp-login.php";
string post_data = "log=test&pwd=asdfz&rememberme=forever&wp-submit=Log+In&redirect_to=http%3A%2F%2Flocalhost%2Fwp%2Fwp-admin%2F&testcookie=1";
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(login_url);
request1.CookieContainer = cookieJar;
request1.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
request1.KeepAlive = true; //this is the default
request1.ContentType = "text/html";
request1.Timeout = 10000;
request1.Method = "GET";
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
StreamReader sr = new StreamReader(response1.GetResponseStream(), Encoding.ASCII);
//MessageBox.Show(sr.ReadToEnd());
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(login_url);
request2.Method = "POST";
request2.CookieContainer = cookieJar;
request2.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
request2.KeepAlive = true;
request2.Timeout = 100000;
request2.AllowAutoRedirect = false;
request2.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray2 = Encoding.ASCII.GetBytes(post_data);
request2.ContentLength = byteArray2.Length;
Stream dataStream2 = request2.GetRequestStream();
dataStream2.Write(byteArray2, 0, byteArray2.Length);
dataStream2.Close();
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
//MessageBox.Show(((HttpWebResponse)response2).StatusDescription);
dataStream2 = response2.GetResponseStream();
StreamReader reader2 = new StreamReader(dataStream2);
string report2 = reader2.ReadToEnd();
//MessageBox.Show(report2);
reader2.Close();
dataStream2.Close();
response2.Close();
HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(admin_url);
request3.Method = "GET";
request3.CookieContainer = cookieJar;
request3.ContentType = "text/html";
request3.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
request3.KeepAlive = true;
request3.Timeout = 100000;
request3.AllowAutoRedirect = true;
HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
StreamReader sr3 = new StreamReader(response3.GetResponseStream(), Encoding.ASCII);
string report3 = sr3.ReadToEnd();
return report3;
}
}
}
But unfortunately in responce I get only HTML source code of login page and it seems that cookies don't contain session ID. All requests which I perform after that code also return HTML source of login page so I can assume that it does not login correctly.

getting response from Facebook is getting timed out

Hi I am using a facebook posts feeding application which will read all the new posts from my facebook page and will save to my sharepoint list. Earlier, i.e, before June it has worked properly it feeds all the posts from fabebook to my Share point list. But nowadays its throws an error while getting response from the Facebook authorize url. I don't know what went wrong. Please help me if you guys have any suggestion to resolve this issue. I am appending my code part below.
private void btnSendToList_Click(object sender, EventArgs e)
{
try
{
if (ConfigurationSettings.AppSettings["fbClientID"] != null)
strClientID = ConfigurationSettings.AppSettings["fbClientID"];
if (ConfigurationSettings.AppSettings["fbRedirectURL"] != null)
strURL = ConfigurationSettings.AppSettings["fbRedirectURL"];
if (ConfigurationSettings.AppSettings["fbCltSecret"] != null)
strCltSecret = ConfigurationSettings.AppSettings["fbCltSecret"];
if (ConfigurationSettings.AppSettings["fbUserId"] != null)
strUserId = ConfigurationSettings.AppSettings["fbUserId"];
if (ConfigurationSettings.AppSettings["SPSiteURL"] != null)
strSiteURL = ConfigurationSettings.AppSettings["SPSiteURL"];
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string postData = String.Format("email={0}&pass={1}", "testuser#gmail.com", "test123$"); // Masking credentials.
getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", strClientID, "http://www.facebook.com/connect/login_success.html"));
getRequest.Method = WebRequestMethods.Http.Get;
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(cookies);
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
//getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
getResponse = (HttpWebResponse)getRequest.GetResponse();
The above line throws WebExceptopn which is teeling like Process has timed out.
strAccessToken = getResponse.ResponseUri.Query;
strAccessToken = strAccessToken.Replace("#_=_", "");
strAccessToken = strAccessToken.Replace("?code=", "");
newStream.Close();
if (!string.IsNullOrEmpty(strAccessToken))
strCode = strAccessToken;
if (!string.IsNullOrEmpty(strCode) && !string.IsNullOrEmpty(strClientID) &&
!string.IsNullOrEmpty(strURL) && !string.IsNullOrEmpty(strCltSecret) &&
!string.IsNullOrEmpty(strUserId))
{
SaveToList();
}
}
catch (Exception ex)
{
LogError(ex);
}
}
Hi Finally i got the solution.
Here i am using getResponse = (HttpWebResponse)getRequest.GetResponse();
The problem is we can use only one HttpWebResponse at a time. In my case i am using the same object in two times without any disposing and closing. That's make me the error.
So I updated my code like this.
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getresponse.Close();
This solved my error. Thanks

WebClient/HttpRequest Navigating To Page After Login

I want to login to a website using a webclient or httpwebrequest, after i'm logged in i want to navigate to a page in the site.
so i know how to post data and to login, my problem is with the navigation after the login,
it redirect me to the login page, i know it has to do with the cookies, but i dont understand what i'm doing worng... here is my code -> it is taken from another answear on stackoverfllow... (http://stackoverflow.com/questions/8425593/c-sharp-httpwebrequest-post-login-to-facebook)
string getUrl = "somesite.com";
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl2 = "somepage.login.com";
string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//untill here it works fine i'm logged in....
HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
getRequest2.CookieContainer = new CookieContainer();
getRequest2.CookieContainer.Add(cookies);
HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();
cookies = getResponse2.Cookies;
using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//here it rediects me to login page
any suggestions> what i'm doing wrong
Do not instantiate CookieContainer for each request. You need one CookieContainer over all requests. Try this:
CookieContainer cc = new CookieContainer();
string getUrl = "somesite.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string getUrl2 = "somepage.login.com";
string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
getRequest.CookieContainer = cc;
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//untill here it works fine i'm logged in....
HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
getRequest2.CookieContainer = cc;
HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();
using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//here it rediects me to login page

Send post to java servlet using C# HttpWebRequest

I have task of uploading file to java servlet. I installed Fiddler to see where web requests are sent and what post data is sent. After logging into java servlet using HttpWebRequest GET method I receive in cookies SessionId. So I was using this SessionId in headers to create POST request to the web server where servlet is. But in response I receive message that "session is time out. Try to login again." But if I use application through user interface I have the one SessionId for all application which is sent in headers with each request.
This application is running in bank so I was thinking if they have some security against scraping.
Am I thinking in a right way? Any help will be appreciated.
Thanks, Elena
Here is my code
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://go.tkygw.pcnet.smbc.local/MgbTokyoGateway/Mgblinkmenu.aspx");
req.Credentials = new NetworkCredential("GB54326", "elena83", "TKYGW");
req.CookieContainer = cookieContainer;
req.Headers.Add("Pragma", "no-cache");
req.Headers.Add("Accept-Language", "en-gb");
req.ProtocolVersion = HttpVersion.Version10;
req.AllowAutoRedirect = true;
WebResponse resp = req.GetResponse();
//here in cookies I receive ASP.NET_session_Id and tkygw_intra
HttpWebResponse webr = (HttpWebResponse)resp;
StreamReader r = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8);
string res = r.ReadToEnd();
resp.Close();
NameValueCollection nvc = new NameValueCollection();
nvc.Add("_PAGEID", "MWMAL1000P00");
nvc.Add("_SENDTS", "1296208904759");
nvc.Add("_TRANID", "AL1000T00P01");
nvc.Add("_SUBINDEX", "-1");
nvc.Add("_TARGET", "");
nvc.Add("_FRAMID", "");
nvc.Add("_LUID", "1296208904720");
nvc.Add("_WINID", "root");
nvc.Add("_TARGETWINID", "TIMEOUTW_300000_13");
nvc.Add("CHK_FLG", "0");
nvc.Add("BUTTON_NAME", "Corporate Card");
nvc.Add("TITLE_NAME", "[AL1000]Main Menu");
nvc.Add("DateFormat", "1");
nvc.Add("BIZKEY", "AC");
nvc.Add("H_REG_NUM", "");
nvc.Add("H_TODO_DISP_MODE", "");
nvc.Add("H_VIEW_CHANGE_FLG", "1");
nvc.Add("H_SMVA_FLG", "0");
nvc.Add("H_SWITCH_ID", "8837");
nvc.Add("T_BOOKING", "8802");
nvc.Add("T_CUSTOMER_ID", "109732");
nvc.Add("P_DATE_FM", "1");
nvc.Add("D_BA_CREDIT_MONITORING_DISABLED", "");
nvc.Add("D_BA_CREDIT_APPLICATION_DISABLED", "");
nvc.Add("D_BA_CREDIT_APPLICATION_DISABLED", "");
nvc.Add("P_BLANKET_APPLI", "");
HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create("http://gcm.tkygw.pcnet.smbc.local/gcmv0/WACSServlet");
//here in cookiesContainer are 4 values: ASP.NET_session_Id , tkygw_intra
req3.CookieContainer = cookieContainer;
req3.Method = "POST";
req3.Accept = "*/*";
// req3.Headers.Add("Pragma", "no-cache");
// req3.Headers.Add("Accept-Language", "en-gb");
req3.AllowAutoRedirect = true;
req3.KeepAlive = true;
req3.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
req3.ContentType = "application/x-www-form-urlencoded";
req3.ProtocolVersion = HttpVersion.Version10;
var sbPostData = new StringBuilder();
if (nvc != null)
{
foreach (string key in nvc.AllKeys)
{
string[] values = nvc.GetValues(key);
if (values != null)
{
foreach (string value in values)
{
if (!string.IsNullOrEmpty(value))
sbPostData.Append(string.Format("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)));
}
}
}
}
var parameterString = Encoding.UTF8.GetBytes(sbPostData.ToString());
req3.Referer = "http://gcm.tkygw.pcnet.smbc.local/gcmv0/WACSServlet?_TRANID=AL0010P01C01";
req3.ContentLength = sbPostData.ToString().Length;
using (Stream requestStream = req3.GetRequestStream())
{
requestStream.Write(parameterString, 0, parameterString.Length);
requestStream.Close();
//nothig is received in cookies. Status of response 200 (OK), but on the web page is error that Session is Time OUT. Please Login again
using (var response = req3.GetResponse() as HttpWebResponse)
{
using (var stIn = new System.IO.StreamReader(response.GetResponseStream()))
{
//here I receive session Time Out. Please login again
string s = stIn.ReadToEnd();
}
}
}
If you are uploading file using HttpWebRequest, you must specify content headers correct.
Content must be specified as Content-Type: multipart/form-data
sample snippet
string DataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
string contentType = "multipart/form-data; boundary=" + DataBoundary ;
req.Method = "POST";
req.ContentType = contentType ;
req.UserAgent = userAgent;
req.CookieContainer = new CookieContainer();
req.ContentLength = formData.Length;
Check out this and this posts for more detail explanation

Categories