zoukankan      html  css  js  c++  java
  • c# http请求接口

    /// <summary>
            /// 不做catch处理,需要在外部做
            /// </summary>
            /// <param name="url"></param>
            /// <param name="method">默认GET,空则补充为GET</param>
            /// <param name="contenttype">默认json,空则补充为json</param>
            /// <param name="header">请求头部</param>
            /// <param name="data">请求body内容</param>
            /// <returns></returns>
            public static string Http(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
                request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
                if (header != null)
                {
                    foreach (var i in header.Keys)
                    {
                        request.Headers.Add(i.ToString(), header[i].ToString());
                    }
                }
                if (!string.IsNullOrEmpty(data))
                {
                    Stream RequestStream = request.GetRequestStream();
                    byte[] bytes = Encoding.UTF8.GetBytes(data);
                    RequestStream.Write(bytes, 0, bytes.Length);
                    RequestStream.Close();
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream ResponseStream = response.GetResponseStream();
                StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
                string re = StreamReader.ReadToEnd();
                StreamReader.Close();
                ResponseStream.Close();
                return re;
            }

    http请求,接口又重新调整了,好用

  • 相关阅读:
    第 02 组 每周小结 (2/3)
    第02组 每周小结(1/3)
    第02组 beta冲刺总结
    第02组 beta冲刺(5/5)
    第02组 beta冲刺(4/5)
    第02组 beta冲刺(3/5)
    第02组 beta冲刺(2/5)
    第02组beta冲刺(1)
    将博客搬至CSDN
    制作鼠标放在图片上,一闪而过水波的效果
  • 原文地址:https://www.cnblogs.com/huanyun/p/11447207.html
Copyright © 2011-2022 走看看