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;
}
Related
I am working on calling asmx webservice method without adding service reference as asmx doesn't provide wsdl. I can access asmx service from fiddler as well as from postman. It works fine. However when I try to call it from my C# code it throws 403 forbidden exception.
Below is my c# code.
internal class Class3
{
string _soapEnvelope =
#"<soap:Envelope
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body></soap:Body></soap:Envelope>";
private string CreateSoapEnvelope()
{
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("aId", "11"); // Add parameterName & Value to dictionary
Params.Add("cId", "22");
Params.Add("lId", "20");
string MethodCall = "<" + "GetSettings" + #" xmlns=""http://tempuri.org/"">";
string StrParameters = string.Empty;
foreach (var param in Params)
{
StrParameters += string.Format("<{0}>{1}</{0}>", param.Key, param.Value);
}
MethodCall = MethodCall + StrParameters + "</" + "GetSettings" + ">";
StringBuilder sb = new StringBuilder(_soapEnvelope);
sb.Insert(sb.ToString().IndexOf("</soap:Body>"), MethodCall);
return sb.ToString();
}
private HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("sample.asmx");
webRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/" + "GetSettings" + "\"");
webRequest.Headers.Add("To", "sample.asmx");
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
webRequest.Host = "sample.com";
webRequest.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
webRequest.Headers.Add("Upgrade-Insecure-Requests: 1");
webRequest.Headers.Add("Origin", "null");
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,zh;q=0.6");
webRequest.Method = "POST";
return webRequest;
}
public string InvokeService()
{
try
{
WebResponse response = null;
string strResponse = "";
//Create the request
HttpWebRequest req = this.CreateWebRequest();
//write the soap envelope to request stream
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(this.CreateSoapEnvelope());
}
}
//get the response from the web service
response = req.GetResponse();
Stream str = response.GetResponseStream();
StreamReader sr = new StreamReader(str);
strResponse = sr.ReadToEnd();
return HttpUtility.HtmlDecode(strResponse);
}
catch (Exception ex)
{
return "";
}
}
}
internal class Program
{
static void Main(string[] args)
{
new Class3().InvokeService();
}
}
Tried adding below headers as well but it doesnt make any difference.
webRequest.ContentLength = 600000;
webRequest.ProtocolVersion = HttpVersion.Version11;
webRequest.KeepAlive = false;
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36";
I am using VS2022 - Console Application with .Net Framework 4.6.2
Can somebody please help me understand what am I doing wrong here.
after lot of research I got to know that the way I am passing parameters were incorrect. Below is the simple and correct version of doing same. Its working now
string remoteUrl = "service.asmx";
string poststring = "aId=11&cId=22&lId=20";
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
httpRequest.Host = "host.com";
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.127 Safari/537.36";
// Convert the post string to a byte array
byte[] bytedata = System.Text.Encoding.UTF8.GetBytes(poststring);
httpRequest.ContentLength = bytedata.Length;
// Create the stream
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
// Get the response from remote server
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sb.Append(line);
}
}
string data = sb.ToString();
I've a problem with a C# HTTP GET request.
I send two requests to server:
The first request returns the value of JSESSIONID and it's work:
private string GetSessionId(){
{
string id = null;
try
{
var webRequest = WebRequest.Create("http://www.xxxxxxxxxxxxxxx.it/home") as HttpWebRequest;
var response = "";
webRequest.Date = DateTime.UtcNow;
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webRequest.Headers.Add("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de-DE;q=0.6,de;q=0.5");
webRequest.Connection = "keep-alive";
webRequest.Headers.Add("Cache-Control", "max-age=0");
webRequest.Host = "www.xxxxxxxxxxxxxxxx.it";
webRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
if (webRequest != null)
{
webRequest.Method = "GET";
using (WebResponse resp = webRequest.GetResponse())
{
using (Stream s = resp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
id = resp.Headers["Set-Cookie"];
response = sr.ReadToEnd();
Console.WriteLine("Response1> " + resp.Headers);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return id;
}
The second request should return the server response to my search but it return an error "302 Moved Temporarily" or "Attention the session has expired".
private void RequestFromServer(string url)
{
string session = GetSessionId().Split(';')[0];
Console.WriteLine("Session> {0}",session);
try
{
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
var response = "";
if (webRequest != null)
{
Uri target = new Uri(url);
webRequest.Method = "GET";
webRequest.Timeout = 20000;
webRequest.Date = DateTime.UtcNow;
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webRequest.Headers.Add("Accept-Language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de-DE;q=0.6,de;q=0.5");
webRequest.Headers.Add("Cache-Control", "max-age=0");
webRequest.Connection = "keep-alive";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Add("Cookie", session);
webRequest.Host = "www.xxxxxxxxxxxxxx.it";
webRequest.Headers.Add("Origin", "http://www.xxxxxxxxxxxx.it");
webRequest.Referer = "http://www.xxxxxxxxxxxxxxxx.it/ricerca/atto/contratti/originario?reset=true&normativi=false";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
webRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
var res = webRequest.GetResponse();
using (Stream s = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
response = sr.ReadToEnd();
Console.Write(res.Headers);
Console.WriteLine(response);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Can someone help me with this problem?
What you probably need here is a CookieContainer. Instead of
webRequest.Headers.Add("Cookie", session);
you should do something like
webRequest.CookieContainer = new CookieContainer();
Cookie cookie = new new Cookie("JSESSIONID", yourSessionIdHere);
// adjust domain accordingly
cookie.Domain = "www.xxxxxxxxxxxxxxx.it";
httpRequest.CookieContainer.Add(cookie);
Code is untested.
The MSDN site also states:
For security reasons, cookies are disabled by default. If you want to use cookies, use the CookieContainer property to enable cookies.
That means in order to use cookies you have to set the CookieContainer explicitly.
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.
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.
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