zoukankan      html  css  js  c++  java
  • C# 请求Https

    /// <summary>
            /// Get请求
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="postDataStr"></param>
            /// <returns></returns>
            public static string HttpGet(string Url, string postDataStr)
            {
                try
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
                    request.Proxy = null;
                    request.KeepAlive = false;
                    request.Method = "GET";
                    request.ContentType = "application/json; charset=UTF-8";
                    request.AutomaticDecompression = DecompressionMethods.GZip;
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                    string retString = myStreamReader.ReadToEnd();
    
                    myStreamReader.Close();
                    myResponseStream.Close();
    
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
    
                    return retString;
                }
                catch (Exception ex)
                {
                    return "";
                }
    
            }
    
            public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            { // Always accept
                Console.WriteLine("accept " + certificate.GetName());
                return true; //总是接受
            }


     /// <summary>
            /// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");
            /// </summary>
            /// <param name="url"></param>
            /// <param name="body"></param>
            /// <returns></returns>
            public static string HttpPost(string url, string body)
            {
                try
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    
                    Encoding encoding = Encoding.UTF8;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "POST";
                    request.Accept = "text/html, application/xhtml+xml, */*";
                    request.ContentType = "application/json";
    
                    byte[] buffer = encoding.GetBytes(body);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    return "";
                }
            }
    
    
            /// <summary>
            /// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");
            /// </summary>
            /// <param name="url"></param>
            /// <param name="body"></param>
            /// <returns></returns>
            public static string HttpPut(string url, string body)
            {
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "Put";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
    
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
    
            /// <summary>
            /// post请求 string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");
            /// </summary>
            /// <param name="url"></param>
            /// <param name="body"></param>
            /// <returns></returns>
            public static string HttpDelete(string url, string body)
            {
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "Delete";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
    
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
    

      

      

  • 相关阅读:
    strong和copy的区别
    xib托线出来的为什么是weak而不是strong
    iOS 序列化与反序列化
    iOS开发- 蓝牙后台接收数据(BLE4.0)
    iOS开发拓展篇-XMPP简单介绍
    OS开发拓展篇—应用之间的跳转和数据传
    ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
    什么是低8位?
    Eclipse自动生成作者、日期注释等功能设置
    eclipse重构变量名的快捷键, 批量修改变量名的快捷键
  • 原文地址:https://www.cnblogs.com/liangwenchao-912/p/8604201.html
Copyright © 2011-2022 走看看