Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
I forgot my password
Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 21st, 2005, 04:02 PM
Authorized User
Points: 325, Level: 6
Points: 325, Level: 6 Points: 325, Level: 6 Points: 325, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Altamonte Springs, FL
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default WebRequest/WebResponse form submit problem. HELP!

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;
  }
}
__________________
http://www.softlinksys.com
Professional Software Developer since 1994.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 14th, 2006, 03:18 AM
Registered User
 
Join Date: May 2006
Location: , , Pakistan.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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);
            reqst.Flush();
            reqst.Close();

            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);
            result=sr.ReadToEnd();
            using(System.IO.StreamWriter writer=new StreamWriter("C:\\Temp\\checkcheck.html"))
            {
                writer.Write(result);
            }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 22nd, 2010, 11:28 PM
Registered User
 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default req.ContentType = "application/x-www-form-urlencoded";

Hi,

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
"application/x-www-form-urlencoded".

I don't know what else can be used.

Please help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
form submit problem alexscan Beginning PHP 0 January 1st, 2008 07:37 PM
form submit problem/dependent list menus alexscan Pro PHP 0 January 1st, 2008 05: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 04:27 AM



All times are GMT -4. The time now is 07:52 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
© 2011 John Wiley & Sons, Inc.