Monday, July 28, 2008

Programmatically Post to ASP page and read results

I want to be able to POST some values to an external web site's ASP page and then programmatically read the results. The external page has no query string paramaters so I can't just create the url and read the page instead I need to create some web requests and deal with that object. This is how I did it, basically a mash up of what I found on the web but I believe more concise and clear.

//set our data
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "firstname=" + fname.Text;
postData += ("&lastname=" + lname.Text);

//compose the submission
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://somepage.asp");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;

// Post to the form.
StreamWriter swRequestWriter = new StreamWriter(request.GetRequestStream());
swRequestWriter.Write(postData);
swRequestWriter.Close();

//get the response
HttpWebResponse hwrWebResponse = (HttpWebResponse)request.GetResponse();

// Read the response
StreamReader srResponseReader = new
StreamReader(hwrWebResponse.GetResponseStream());
string strResponseData = srResponseReader.ReadToEnd();
srResponseReader.Close();

// Display the response on my page
Response.Write(strResponseData);

Now what I plan to do is screen scrape the result page and get the data I need. Ah regular expressions, I hate those. Stay tuned for that post in the next couple of days...

5 comments:

Unknown said...

Thanks for posting this article.
it helped me a lot.
but i have one doubt.

ASCIIEncoding encoding = new ASCIIEncoding();

Could you tell me what is the perpose of declaring this encoding variable which was not used in above code snippet.

Unknown said...

You can ignore the ASCIIEncoding, it must have been left over from some trial and errors I did before I got it to work correctly. Thanks for the catch.

digital signature workflow said...

The requirement in hand with me also require POST some values to an external web site's ASP page and then programmatically read the results. Is there some other way to write response than Response.Write(strResponseData);
which i can say is more formated.

Anonymous said...

If you prize the correct casino site and target correctly for minimal toll, then you can push back for pocket-size stakes with the hazard for Dandy prizes. [url=http://www.onlinecasinoburger.co.uk/]http://www.onlinecasinotaste.co.uk/[/url] online casino Size and hit are a media contriver's games, toothed wheel is the to the highest degree pop, but in realism none of them actually Run. http://www.onlinecasinoburger.co.uk/

Anonymous said...

Having multiple payday loan will cost you further than imagine from their 401k people assets like several building as well as property [url=http://www.pahiy.co.uk/]one day car insurance[/url] temporary car insurance Always remember that a financial institution will never request personal data with no prompting on your side http://www.pahiy.co.uk/