Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 21st, 2005, 04:02 PM
Authorized User
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default WebRequest/WebResponse form submit problem. HELP!


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:

If I log in as: login: Mister Pwd: Tester
Using the IE browser it returns a URL of:
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:
private void Page_Load(object sender, System.EventArgs e)

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"];  

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();

  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 
  sHTML = loResponseStream.ReadToEnd(); 

  if(sHTML != "")
    didSubmit = true;
Professional Software Developer since 1994.
Old June 14th, 2006, 03:18 AM
Registered User
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts

well fellow ur problem is solved. iam sending u the code. implement it.
string url="http://www.365articles.com/modules.php?name=Your_Account";
            string data="username=gogo&user_password=newnewne&op=logi n";
            byte[] buffer=Encoding.UTF8.GetBytes(data);
            string result="";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType ="application/x-www-form-urlencoded";
            req.ContentLength = buffer.Length;
            //req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
            req.CookieContainer = new CookieContainer(); // enable cookies

            Stream reqst = req.GetRequestStream(); // add form data to request stream

            reqst.Write(buffer, 0, buffer.Length);

            Console.WriteLine("\nPosting data to " + url);
            HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // send request,get response
            Console.WriteLine("\nResponse stream is: \n");
            Stream resst = res.GetResponseStream(); // display HTTP response
            StreamReader sr = new StreamReader(resst);
            using(System.IO.StreamWriter writer=new StreamWriter("C:\\Temp\\checkcheck.html"))
Old December 23rd, 2010, 12:28 AM
Registered User
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default req.ContentType = "application/x-www-form-urlencoded";


I have been scrapping sites.But today I encounterred a new problem.

req.ContentType = "application/x-www-form-urlencoded";

Actually the content type which is accepted by server is not

I don't know what else can be used.

Please help

Similar Threads
Thread Thread Starter Forum Replies Last Post
form submit problem alexscan Beginning PHP 0 January 1st, 2008 08:37 PM
form submit problem/dependent list menus alexscan Pro PHP 0 January 1st, 2008 06:29 PM
Form Submit Problem. muklee Javascript 7 May 27th, 2007 10:06 PM
Firefox Problem - submit form mattastic Javascript How-To 4 July 19th, 2005 07:30 AM
form submit problem dhaywirex Javascript How-To 2 March 29th, 2004 05:27 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.