ASP uses WebRequest to implement cross-domain requests

Source: Internet
Author: User

Two days ago there was a sudden need to use cross-domain submission data. I also found a lot of code examples on the Internet to see. JSONP is commonly used to implement cross-domain, but the JSONP value supports get mode submission. This caused the problem, and later my colleague told me to use the server to do that is WebRequest to achieve. I researched it myself and came to write an article summarizing my experience.

I hope I can share it with you.

The first is the data transfer of the HTML page, the way I submit it, I don't have to say, Ajax forms can be. Focus on the background code how to transfer data to another site.

1         PublicActionResult Openreadwithhttps (stringUsernamestringEmailstringSex)2         {3             stringURL ="https://***********************"Here is the address you requested or the one that was provided to you, and the format that was submitted. 4             varJSS =NewJavaScriptSerializer ();//This can be understood as turning the model into aJSON string6             varModel =NewUsermodel ();7Instantiate model assigns a value to the model and then goes to invoke.9Model. Username =username; OneModel. Email =email; -Model. Sex =sex; +            stringtemp =httppost (URL, JSS. Serialize (model)); A             returnJson (temp, jsonrequestbehavior.allowget); at}

The next piece of code is the point.

private string HttpPost (String Url, String postdatastr) {HttpWebRequest request = (HttpWebRequest) webre Quest.            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 transfer your parameters to the corresponding interface in a WebRequest way.

I just started using the gbk2312 format to commit to the interface. After the submission my side returned to the success. But the Chinese character that the interface holds is a bunch of garbled characters. and change it to UTF-8.

Changed to UTF-8 after the problem came, my side of the data on the transmission is not past. I was kneeling here. To the internet for half a day and did not find the answer you want. Then I'll try to comment on the three lines of the converted format code (commented on that method above).

I didn't think it was successful after the comment. Luck is also part of the strength. And then went to find out why the value is converted after the error. The answer is that the length of the string conversion is actually lost. Will be lost. Lost.. Lost.. That is, it will be a few bytes less than before the conversion. So I can't submit ...

In fact, after doing it again, it feels simple to listen. Cross-domain submission requests It feels like a little bit tall, actually.

(I hope this article is helpful to you)

ASP uses WebRequest to implement cross-domain requests

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.