ASP uses webRequest to implement cross-origin requests and aspwebrequest to implement cross-origin requests

Source: Internet
Author: User

ASP uses webRequest to implement cross-origin requests and aspwebrequest to implement cross-origin requests

Two days ago, we suddenly had a need to use cross-Origin data submission. I also found a lot of code examples on the Internet. Jsonp is commonly used to implement cross-origin, but jsonp supports the get Method for submission. This caused the problem. Later, my colleague told me that I could use the server to implement webRequest. I studied it myself and wrote an article to summarize my experience.

I hope you can share it with us.

 

The first is the data transmission of html pages. I don't need to talk about this submission method. ajax forms can all be used. The focus is on how the background code transmits data to another site.

 

1 public ActionResult OpenReadWithHttps (string username, string email, string sex) 2 {3 string url = "https: //***********************"; // here is the address you requested or the interface provided to you, and the format will be submitted at the same time. 4 var jss = new JavaScriptSerializer (); // This can be understood as converting the model into a json string 6 var model = new UserModel (); 7 // instantiate the model and assign a value to the model, then call. 9 model. username = username; 11 model. email = email; 13 model. sex = sex; 19 string temp = HttpPost (url, jss. serialize (model); 22 return Json (temp, JsonRequestBehavior. allowGet); 23}

The following code is the focus ..

 

       private string HttpPost(string Url, string postDataStr)        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);            request.Method = "POST";            request.ContentType = "application/json";

//request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); //var str = Encoding.GetEncoding("UTF-8"); //request.ContentLength = str.GetByteCount(postDataStr);

Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("UTF-8")); myStreamWriter.Write(postDataStr); myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }

The above method is to transmit your parameters to the corresponding interface in webRequest mode.

In this way, I first used the gbk2312 format to submit it to the interface. After submission, the returned result is successful. However, the Chinese characters stored on the interface are garbled .. Then change to UTF-8

After changing the UTF-8, the problem came, and the data on my side could not be transmitted .. At that time, I was kneeling here. After searching for the Internet for half a day, I didn't find the answer I wanted. Then I will try to annotate the three lines of code in the conversion format (as mentioned above ).

I did not expect that the comment was successful .. Luck is also a part of strength .. Then, find out why an error occurs after the value is converted .. The answer is that the length will be lost during String Conversion. Will be lost .. Lost .. .. That is, it will be several bytes less than before conversion .. So I cannot submit it...

In fact, it is easy to hear after this operation. Cross-origin request submission seems to be a little too powerful.

(I hope this article will be helpful to you)

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.