Request a Cross-domain page with the WebRequest method and post a large amount of data

Source: Internet
Author: User

In a recent project, you need to access another Web page across domains and require incoming data, such as a large amount of data to be passed in, that passes through the post data method, because the Get method can only transfer data within 2KB.
Here's how to share this functionality:

#region request to execute a Remote Web page and send data
<summary>
Request to execute a Remote Web page and send data
</summary>
<param name= "Pageurl" ></param>
<param name= "MSG" ></param>
<param name= "Reqtype" >1 for get,2 post</param>
<returns></returns>
public static string Requesturl (string pageurl, string msg, int reqtype)
{
String strquery = "Senddata=" + crypt.encrypt (msg); Encrypt the transmitted data
String returnvalue = "";
if (Reqtype = 2)
{
WebRequest wrequest = WebRequest.Create (Pageurl);
Wrequest.timeout = 8000;
Wrequest.method = "POST";
Wrequest.contenttype = "application/x-www-form-urlencoded";
string datasend = strquery;
Wrequest.contentlength = Datasend.length;

byte[] Buff = Encoding.UTF8.GetBytes (datasend);
Stream Reqstream = Wrequest.getrequeststream ();
Reqstream.write (buff, 0, buff. Length);
Reqstream.close ();

WebResponse wresponse = Wrequest.getresponse ();
Stream Repstream = Wresponse.getresponsestream ();
StreamReader sr = new StreamReader (Repstream, encoding.getencoding ("GB2312"));
returnvalue = Sr. ReadToEnd (). ToString ();
Sr. Close ();
}
else if (Reqtype = 1)
{
WebRequest wrequest = webrequest.create (Pageurl + "?" + strquery);
Wrequest.timeout = 8000;
WebResponse wresponse = Wrequest.getresponse ();
Stream Resstream = Wresponse.getresponsestream ();
StreamReader sr = new StreamReader (Resstream, System.Text.Encoding.GetEncoding ("GB2312"));
returnvalue = Sr. ReadToEnd (). ToString ();
Resstream.close ();
Sr. Close ();
}
Return returnvalue;
}
#endregion

This approach is convenient for performing remote Web pages and Cross-domain access to Web pages.
Note To reference the using System.Net namespace.

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.