I originally wanted to use jsonp for cross-origin requests, but sb writes the interface and does not change the return value... I have to request the interface in the background..., jsonpsb

Source: Internet
Author: User

I originally wanted to use jsonp for cross-origin requests, but sb writes the interface and does not change the return value... I have to request the interface in the background..., jsonpsb

String url = "ssss.com ";
// In the demo, the get request HttpWebResponse w = HttpHelper. createGetHttpResponse (url, 30000, null, null); StreamReader sr = new StreamReader (w. getResponseStream (); string/* This is a Json string */jsonstr = sr. readToEnd ();

 

Using System; using System. collections. generic; using System. IO; using System. net; using System. net. security; using System. security. cryptography. x509Certificates; using System. text; public class HttpHelper {/// <summary> /// create an HTTP request in GET mode //</summary> /// public static HttpWebResponse CreateGetHttpResponse (string url, int timeout, string userAgent, CookieCollection cookies) {HttpWebRequest request = n Ull; if (url. startsWith ("https", StringComparison. ordinalIgnoreCase) {// verify the validity of the server certificate (a certificate issued by a non-third-party authority, such as a self-generated certificate, without verification, returns true here) ServicePointManager. serverCertificateValidationCallback = new RemoteCertificateValidationCallback (CheckValidationResult); request = WebRequest. create (url) as HttpWebRequest; request. protocolVersion = HttpVersion. version10; // http version. The default value is 1.1. Set this parameter to 1.0} else {request = WebReque. St. create (url) as HttpWebRequest;} request. method = "GET"; // set the proxy UserAgent and timeout // request. userAgent = userAgent; // request. timeout = timeout; if (cookies! = Null) {request. cookieContainer = new CookieContainer (); request. cookieContainer. add (cookies);} return request. getResponse () as HttpWebResponse;} // <summary> // create an HTTP request in POST mode /// </summary> public static HttpWebResponse createpostpresponse (string url, IDictionary <string, string> parameters, int timeout, string userAgent, CookieCollection cookies) {HttpWebRequest request = null; // If sent HTTPS request if (url. startsWith ("https", StringComparison. ordinalIgnoreCase) {// ServicePointManager. serverCertificateValidationCallback = new RemoteCertificateValidationCallback (CheckValidationResult); request = WebRequest. create (url) as HttpWebRequest; // request. protocolVersion = HttpVersion. version10;} else {request = WebRequest. create (url) as HttpWebRequest;} request. method = "POST"; request. co NtentType = "application/x-www-form-urlencoded"; // set the proxy UserAgent and timeout // request. userAgent = userAgent; // request. timeout = timeout; if (cookies! = Null) {request. CookieContainer = new CookieContainer (); request. CookieContainer. Add (cookies) ;}// send POST data if (! (Parameters = null | parameters. count = 0) {StringBuilder buffer = new StringBuilder (); int I = 0; foreach (string key in parameters. keys) {if (I> 0) {buffer. appendFormat ("& {0 }={ 1}", key, parameters [key]);} else {buffer. appendFormat ("{0} = {1}", key, parameters [key]); I ++ ;}} byte [] data = Encoding. ASCII. getBytes (buffer. toString (); using (Stream stream = request. getRequestStream () {stream. write (data, 0, data. length) ;}} string [] values = request. headers. getValues ("Content-Type"); return request. getResponse () as HttpWebResponse;} // <summary> // obtain the request data /// </summary> public static string GetResponseString (HttpWebResponse webresponse) {using (Stream s = webresponse. getResponseStream () {StreamReader reader = new StreamReader (s, Encoding. UTF8); return reader. readToEnd () ;}/// <summary> // verify the certificate /// </summary> private static bool CheckValidationResult (object sender, X509Certificate certificate, X509Chain chain, sslPolicyErrors errors) {if (errors = SslPolicyErrors. none) return true; return false ;}}

 

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.