Hi:
I'm trying to make a post request to a site.
If I use a normal browser I get a different response than if I use HttpWebRequest.
This is one example. I'm seemingly not successfully posting to any of these sites. I created a site of my own that would read the request vars and return a response. That one works fine but this other one doesn't.
The site:
http://www.365articles.com/modules.p...e=Your_Account
If I log in as: login: Mister Pwd: Tester
Using the IE browser it returns a URL of:
http://www.365articles.com/modules.p...Account&stop=1
and I see text:
"Login Incorrect! Please Try Again..."
If I try the same thing using HttpWebRequest & HttpWebResponse it just returns to the URL I submitted with nothing changed.
What the heck am I doing wrong?
Why does the browser return with a different URL and different text but this does not?
Here's my code:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
getCookie();
}
private void getCookie()
{
string sURL = "http://www.365articles.com/modules.php?name=Your_Account";
HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(sURL);
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.KeepAlive = false;
webrequest.Method = "get";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
WebHeaderCollection headers = webresponse.Headers;
if (headers["Set-Cookie"] != null)
{
Cookie = headers["Set-Cookie"];
}
doSubmit();
}
private void doSubmit()
{
string uri = "";
string sURL = "http://www.365articles.com/modules.php?name=Your_Account";
string sRequest = "username=Mister&user_password=Tester&redirect=&mode=&f=&t=&op=login&submit=Login";
HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(sURL);
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.KeepAlive = false;
webrequest.Method = "post";
webrequest.Headers.Add("Cookie", Cookie);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(sRequest);
webrequest.ContentLength = bytes.Length;
Stream oStreamOut = webrequest.GetRequestStream();
oStreamOut.Write(bytes,0,bytes.Length);
oStreamOut.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
WebHeaderCollection headers = webresponse.Headers;
if ((webresponse.StatusCode == HttpStatusCode.Found) ||
(webresponse.StatusCode == HttpStatusCode.Redirect) ||
(webresponse.StatusCode == HttpStatusCode.Moved) ||
(webresponse.StatusCode == HttpStatusCode.MovedPermanently))
{
// Get redirected uri
uri = headers["Location"] ;
uri = uri.Trim();
}
Response.Write(uri + "<BR>");
//Check for any cookies
if (headers["Set-Cookie"] != null)
{
Cookie = headers["Set-Cookie"];
}
Response.Write(Cookie + "<BR>");
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream = new
StreamReader(webresponse.GetResponseStream(),enc);
sHTML = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
if(sHTML != "")
{
didSubmit = true;
}
}