Q: How to request a remote Web site in an existing ASP.net page, and to be able to pass the parameters and get the results of the request response. Use the following small example to explain the implementation of specific functions:
First, we want to request a remote site that needs to use the HttpWebRequest class, which is in the System.Net namespace, so it needs to be referenced. In addition, stream flow operations are required to write parameters to the requested page, so reference to the System.IO namespace is required.
The following is a GET request method:
Uri uri = new Uri ("http://www.cnsaiko.com/");//Create URI object specifying the address if (URI) to be requested
. Scheme.equals (Uri.urischemehttp))//Verify that the Uri is accessed with the HTTP protocol
{
HttpWebRequest request = (HttpWebRequest) Httpwebrequest.create (URI);//Creates an object with a request to the URI using the Create method of the HttpWebRequest class.
request. METHOD = webrequestmethods.http.get;//Specifies the way the request is
httpwebresponse response = (HttpWebResponse) requests for the Get mode. GetResponse ()//Gets the resource that the request responds to, and strongly transitions to HttpWebResponse response object
StreamReader reader = new StreamReader (response. GetResponseStream ());//Gets the readable stream
string str = reader for the response object. ReadToEnd (); Reads the stream text and assigns the value to the STR
response. Close (); Close response
Response.Write (str);//This page output the text content
Response.End ()//This page response end.
}
The following is the POST request method:
Uri uri = new Uri ("Http://www.cnsaiko.com/Admin/Login.aspx?type=Login");//Create URI object that specifies the address to request.
Note that the requested address is the action address of the form form. if (URI. Scheme = = uri.urischemehttp)//Verify that the Uri accesses {string name = Server.URLEncode ("John") with the HTTP protocol;//The parameters to be passed are URL-edited
Code string pwd = Server.URLEncode ("123"); String data = "Username=" + name + "&userpwd=" + pwd;
Data is the parameter to be passed, the name of the form element precedes the = number, the value to be assigned, and the & connection if the argument is multiple.
HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URI); Request. METHOD = webrequestmethods.http.post;//Specifies the way the request is to be Post. ContentLength = data. Length; Specifies the length of the argument to request. ContentType = "application/x-www-form-urlencoded"; Specifies the requested content type StreamWriter writer = new StreamWriter (request. GetRequestStream ()); The write stream writer the request is created with the request object. Write (data); Writes the requested parameter list to the request object writer. Close ();
Closes the write stream. HttpWebResponse response = (HttpWebResponse) rEquest.
GetResponse (); StreamReader reader = new StreamReader (response.
GetResponseStream ()); String str = reader.
ReadToEnd (); Response.
Close ();
Response.Write (str);
Response.End (); }