How to request a remote Web site in the asp.net page

Source: Internet
Author: User
Tags urlencode

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 (); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.