For a detailed description (http://chs.gotdotnet.com/quickstart/howto/doc/WebRequests/clientPOST.aspx) in Microsoft's. Net quick start tutorial on how to use the webrequest and webresponse classes to generate POST requests on Uris, a brief reference is as follows:
The webresponse class is an abstract (mustinherit in Visual Basic) base class. Protocol-specific response classes are derived from this abstract base class. Applications can use instances of the webresponse class to participate in request and response transactions in an unknown way, while protocol-specific classes derived from webresponse carry request details.
The client application does not directly create a webresponse object, but creates it by calling the getresponse method on the webrequest instance.
Description of successors: When inheriting from webresponse, the following Members must be rewritten: contentlength, contenttype, getresponsestream, responseuri, and headers.
If you need to use the webrequest and webresponse classes to generate GET requests on Uris. You can find the specific details of the request generated in the getpage function. The getpage function uses a string parameter, which is the URL (or Uri) of the web page you requested ). Then, this URI is included as a parameter in the webrequest. Create call. This call creates a webrequest object. Then, the getresponse function of the webrequest object is used to obtain the webresponse object. This object can be used to obtain the response status code and the actual response stream (for example, web pages ). The stream can be written in several different forms.
After processing the response stream, make sure to call the close method of the webresponse object to avoid leaking valuable system resources.
If you need to use the webrequest and webresponse classes to generate POST requests on the URI. You can use a simple webrequest with a get predicate. There are two differences:
1) The predicate must be changed to post.
2) The format information needs to be encoded and sent to the stream.
To change the predicate, set the method attribute to "Post ". You must set the contenttype attribute to "application/X-WWW-form-urlencoded ". In this case, make sure that the provided string is correctly encoded and all content is correctly passed (post ).
After processing the response stream, make sure to call the close method of the webresponse object to avoid leaking valuable system resources.
<! -- Body -->
After understanding the specific implementation methods and principles, you can achieve this goal. First, you need a function to implement this function:
/// <Summary>
/// Getpage
/// URL address to be requested
/// Payload input parameter <XML>
/// Outputresp output result
/// </Summary>
Private bool getpage (string URL, string payload, ref string outputresp)
{
Webresponse result = NULL;
Outputresp = "";
Try
{
Webrequest Req = webrequest. Create (URL );
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";
If (payload! = NULL)
{
String urlencoded = payload;
Byte [] somebytes = NULL;
Somebytes = encoding. utf8.getbytes (urlencoded. tostring ());
Req. contentlength = somebytes. length;
Stream newstream = Req. getrequeststream ();
Newstream. Write (somebytes, 0, somebytes. Length );
Newstream. Close ();
}
Else
{
Req. contentlength = 0;
}
Result = Req. getresponse ();
Stream receivestream = result. getresponsestream ();
Encoding encode = system. Text. encoding. getencoding ("UTF-8 ");
Streamreader sr = new streamreader (receivestream, encode );
Outputresp = Sr. readtoend ();
}
Catch (exception E)
{
Console. writeline (E. tostring ());
Console. writeline ("/R/n cannot find the request URI, or its format is incorrect ");
Return false;
}
Finally
{
If (result! = NULL)
{
Result. Close ();
}
}
Return true;
}
This function implements the specific request function. You must enter the request URL and parameters (payload: XML file format), and then webrequest sends a POST request to the service address, the specific result is read by streamreader. The following is an example of using this function:
Private void button_serverclick (Object sender, system. eventargs E)
{
String userregisterreq = "";
Userregisterreq = @ "<? XML version = "" 1.0 "" encoding = "" gb2312 ""?>
<Elink>
<Msgtype> userregisterreq </msgtype>
<Version> 1.0 </version>
<USERPROFILE>
<Username> test </username>
<Userpwd> test </userpwd>
</USERPROFILE>
</Elink> ";
String outputresp = "";
Bool isreg = false;
Isreg = This. getpage (@ "http://dll.test.com/test.dll/", userregisterreq, ref outputresp );
If (isreg)
{
// Outputresp = outputresp. Replace (@ "<", "& lt ;");
// Outputresp = outputresp. Replace (@ ">", "& gt ;");
Response. Write (outputresp );
}
}
Button_serverclick is a button click event. When you enter the correct URL (request address) and payload (input parameter), the return value provided by the Service Program is obtained.