• c# http put请求 通用http请求 post get put


    POST 需要把 contentType 改为 application/x-www-form-urlencoded; charset=UTF-8

    PUT  需要把 contentType 改为application/json

    /// <summary>
            /// 通用请求方法
            /// </summary>
            /// <param name="url"></param>
            /// <param name="datas"></param>
            /// <param name="method">POST GET PUT DELETE</param>
            /// <param name="contentType">"POST application/x-www-form-urlencoded; charset=UTF-8"</param>
            /// <param name="encoding"></param>
            /// <returns></returns>
            public static string HttpRequest(string url, string data, string method = "PUT", string contentType = "application/json", Encoding encoding = null)
            {
    byte[] datas = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);//data可以直接传字节类型 byte[] data,然后这一段就可以去掉 if (encoding == null) encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = method; request.Timeout = 150000; request.AllowAutoRedirect = false; if (!string.IsNullOrEmpty(contentType)) { request.ContentType = contentType; } if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } Stream requestStream = null; string responseStr = null; try { if (datas != null) { request.ContentLength = datas.Length; requestStream = request.GetRequestStream(); requestStream.Write(datas, 0, datas.Length); requestStream.Close(); } else { request.ContentLength = 0; } using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse()) { Stream getStream = webResponse.GetResponseStream(); byte[] outBytes = ReadFully(getStream); getStream.Close(); responseStr = Encoding.UTF8.GetString(outBytes); } } catch (Exception) { throw; } finally { request = null; requestStream = null; } return responseStr; }
    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true; //总是接受  
            }
     public static byte[] ReadFully(Stream stream)
            {
                byte[] buffer = new byte[512];
                using (MemoryStream ms = new MemoryStream())
                {
                    while (true)
                    {
                        int read = stream.Read(buffer, 0, buffer.Length);
                        if (read <= 0)
                            return ms.ToArray();
                        ms.Write(buffer, 0, read);
                    }
                }
            }
  • 相关阅读:
    POJ 1182 食物链 +POJ 2492 A Bug's Life 种类并查集
    Codeforces 884C Bertown Subway dfs判环
    HDU 2102 A计划 bfs
    Codeforces Round #442 (Div. 2) 877E
    Codeforces Round #443 (Div. 2) B. Table Tennis
    Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
    HDU 1495 非常可乐 BFS/数论
    Codeforces Round #442 (Div. 2) B. Nikita and string DP
    51Nod 1102 面积最大的矩形 +1272 最大距离 单调栈
    HDU 1286 找新朋友
  • 原文地址:https://www.cnblogs.com/wangxlei/p/11686013.html
Copyright © 2020-2023  润新知