0
回答
c#发送一个HTTPS的get或者post请求,我在网站上怎么通过node获取这个请求,实现收发数据
华为云4核8G,高性能云服务器,免费试用   
  1. namespace HttpWebRequestDemo  
  2. {  
  3.     class Program  
  4.     {  
  5.         private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";  
  6.   
  7.         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)  
  8.         {  
  9.             return true; //总是接受     
  10.         }  
  11.   
  12.         public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,Encoding charset)  
  13.         {  
  14.             HttpWebRequest request = null;  
  15.             //HTTPSQ请求  
  16.             ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
  17.             request = WebRequest.Create(url) as HttpWebRequest;  
  18.             request.ProtocolVersion = HttpVersion.Version10;  
  19.             request.Method = "POST";  
  20.             request.ContentType = "application/x-www-form-urlencoded";  
  21.             request.UserAgent = DefaultUserAgent;  
  22.             //如果需要POST数据     
  23.             if (!(parameters == null || parameters.Count == 0))  
  24.             {  
  25.                 StringBuilder buffer = new StringBuilder();  
  26.                 int i = 0;  
  27.                 foreach (string key in parameters.Keys)  
  28.                 {  
  29.                     if (i > 0)  
  30.                     {  
  31.                         buffer.AppendFormat("&{0}={1}", key, parameters[key]);  
  32.                     }  
  33.                     else  
  34.                     {  
  35.                         buffer.AppendFormat("{0}={1}", key, parameters[key]);  
  36.                     }  
  37.                     i++;  
  38.                 }  
  39.                 byte[] data = charset.GetBytes(buffer.ToString());  
  40.                 using (Stream stream = request.GetRequestStream())  
  41.                 {  
  42.                     stream.Write(data, 0, data.Length);  
  43.                 }  
  44.             }  
  45.             return request.GetResponse() as HttpWebResponse;  
  46.         }     
  47.   
  48.         static void Main(string[] args)  
  49.         {  
  50.             string url = "https://192.168.1.101/httpOrg/create";  
  51.             Encoding encoding = Encoding.GetEncoding("utf-8");  
  52.             IDictionary<string, string> parameters = new Dictionary<string, string>();  
  53.             parameters.Add("authuser", "*****");  
  54.             parameters.Add("authpass", "*****");  
  55.             parameters.Add("orgkey","*****");  
  56.             parameters.Add("orgname", "*****");  
  57.             HttpWebResponse response = Program.CreatePostHttpResponse(url,parameters,encoding);  
  58.             //打印返回值  
  59.             Stream stream = response.GetResponseStream();   //获取响应的字符串流  
  60.             StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
  61.             string html = sr.ReadToEnd();   //从头读到尾,放到字符串html  
  62.             Console.WriteLine(html);   
  63.         }  
  64.     }  
  65. }  
<无标签>
举报
王大大仙
发帖于11个月前 0回/48阅
顶部