E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Utility Belt: HttpPost
by Gary Pupurs

HttpGrab() is great when you want to screen scrape a URL you've seen in the browser's location box.  But what about POSTs?  How do you screen scrape when you need to submit a remote form via ASP.NET?

Fortunately, Utility Belt also makes this easy!

The code below shows "Utility Belt" submitting form parameters to a third party server, based on the form fields on your page.  The result of the form POST is captured and returned as a string.  Easy!

Tips on building your custom form:

  • On the remote form you want to scrape, do a 'View Source' and cut/paste the code to a page on your server.

  • Pare it down to the basic elements between the <FORM> </FORM> tags.

  • Save as a plain HTML file and try to submit the form. 

  • Keep removing non-essential form elements and retest until you find the minimum fields for your desired results.

  • Then build your ASP.NET form with similar elements, using hidden form elements for any required but non-changing fields.

Note: Some servers may use Javascript, redirection, HTTP header checks or other methods to defeat form submissions from servers other than their own.  You might be able to get around these with some ingenuity.  Or you may not.  YMMV.

   filename=/experiments/utilitybelt/vercurrent/ubdemo_httppost.aspx

<Test Script Below>


<%@ Debug="true" %>
<%@ Assembly Src="utilitybelt.vb" %>
<script runat="server">
Dim ub as New learnasp.UtilityBelt()
Sub Page_Load(Src As Object, E As EventArgs)
    If IsPostback
        dim ht1 as new hashtable
        ht1.add("searchtext",txtSearch.Text)
        ht1.add("searchdatabase",rblNewsSource.SelectedItem.Value)
        lblResults.Text = ub.HttpPost("http://www.washingtonpost.com/cgi-bin/search99.pl",ht1)
    End If
End Sub
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>HttpPost demo</title>
</head>

<body>
<form runat="server">
<h1>Utility Belt HttpPost() Demo</h1>

Enter a word or phrase to search for headlines:<br>
<asp:textbox id="txtSearch" Columns="40" runat="server" />
<br>
<asp:RadioButtonList id="rblNewsSource" runat="server">
            <asp:ListItem value="ap">Associated Press</asp:ListItem>
            <asp:ListItem value="reut">Reuters</asp:ListItem>
</asp:RadioButtonList>


<br>
<input type="submit" value="Do that ASP.NET magic!" />

<hr>
<p>
Results:<br>
<asp:label id="lblResults" runat="server" />
<hr>

<asp:placeholder id="phExceptionInfo" runat="server" />


</form>
</body>

</html>
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.