Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal      

Simulate Web Form POST Request in .NET

Submitted on July 20, 2006

Introduction

This article continues the series of articles on submitting Web Form POST requests from your application: Simulate Web Form Post RequestSimulate a Web Submit Wizard with POST Request

When developing software applications, it may be necessary to organize data exchange between client and server where the client may connect through proxy or firewall.

The traditional ways to implement this functionality, such as FTP or WebDAV, cannot be applied in particular cases. The FTP protocol requires two connections for transferring data between client and server and also requires special writing permissions to the server directories. WebDAV also requires specific access permissions and often can not be activated on web server due to security considerations.

Submit data with a POST request

However, it is still possible to submit data to the server by using standard HTTP POST request.

Suppose we need to submit some textual data to the web server. Web browsers use special <FORM> HTML tags for filling this textual data in GUI and submitting data to the server:

<FORM name="example1" action="submitdata.asp" method="post">
   <INPUT type="text" name="Name">
   <INPUT type="text" name="Birthdate">
   <INPUT type="submit" value="Submit">
</FORM>

We need to perform two actions in order to simulate this POST request in your .NET application:

  • compose the request body;
  • send this request via HTTP.

The easiest way to compose the Web Form POST request is to use the HttpRequest component from the Internet Components - Clever Internet .NET Suite library. The HttpRequest component allows composing requests in both run-time and IDE design-time modes.

The code that does the work:

httpRequest1.Items.AddFormField("Name", "John")
httpRequest1.Items.AddFormField("Birthdate", "10.10.72")

For submitting files using Form-based file upload, we need to add a special Submit File item to the HttpRequest items collection:

httpRequest1.Items.AddSubmitFile("FileName", "c:\uploads\document1.txt")

You can use different sources for creating Web Form POST requests in IDE design-time: an URL which references to the Web page with interested <FORM> tags, a file with stored Web page source or even HTML source lines. Also you can add all necessary request data manually by filling the HttpRequest items collection in IDE designer:

The method of composing request data in IDE design-time is preferable when you need to send non-changing data from request to request.

The second step is to submit composed data to the server. Let's go ahead and use another component from Internet Components - Clever Internet .NET Suite - Http Client:

http1.Post("http://www.website.com/submit.asp", httpRequest1, null);

You can use any preferable HTTP client engine including standard Microsoft .NET System.Web .HttpRequest or your own socket-based implementation of the HTTP protocol.

Submit data with a GET request

There is another method of submitting data to the Web server - by using the HTTP GET request. In contrast to POST, the GET request submits data as a part of requested URL within the URL Query string.

This method is commonly used by search engines for submitting search queries entered by users (see Google, MSN Search, Yahoo). When using GET and Query strings, you can store the whole request as simple text line for future use (e.g., you can copy it to the Windows Clipboard or add to IE Favorites).

However, all data is submitted as clean text and can not be protected even by using SSL. As a result, you can not use the GET request for submitting sensitive data. The GET request data can be combined by using the code below:

HttpRequest1.Items.AddFormField("hl", "en")
HttpRequest1.Items.AddFormField("lr", "")
HttpRequest1.Items.AddFormField("q", edtSearch.Text)

The following code line submits the GET request to the server:

response = Http1.Get("http://www.google.com/search?" + StringUtils.GetStringsAsString(HttpRequest1.RequestSource))

Download Sample Code

This code is constantly refined and improved and your comments and suggestions are always welcome.

Please feel free to Contact Us It will be our pleasure to answer your questions.

Best regards,
Sergey Shirokov 
Clever Components team

    Copyright © 2000-2024