Simulate Web Form POST Request in .NET
Submitted on July 20, 2006
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:
We need to perform two actions in order to simulate this POST request in your .NET application:
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:
For submitting files using Form-based file upload, we need to add a special Submit File item to the HttpRequest items collection:
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.
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:
The following code line submits the GET request to the server:
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.