zoukankan      html  css  js  c++  java
  • C# Get和Post请求接口类

    
    
    public class HttpHelper
        {/// <summary>
            /// Get请求
            /// </summary>
            /// <param name="url">接口地址例如:http://www.jiekou.com?id=123&name=jack</param>
            /// <returns></returns>
            public static string HttpGet(string url)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                //设置请求方法
                httpWebRequest.Method = "GET";
                //请求超时时间
                httpWebRequest.Timeout = 20000;
                //发送请求
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                //利用Stream流读取返回数据
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                //获得最终数据,一般是json
                string responseContent = streamReader.ReadToEnd();
    
                streamReader.Close();
                httpWebResponse.Close();
    
                return responseContent;
    
            }
    
            /// <summary>
            /// Post请求
            /// </summary>
            /// <param name="url">接口地址</param>
            /// <param name="data">json格式参数 例如:{"name":"jack","age":15}</param>
            /// <returns></returns>
            public static string HttpPost(string url, string data)
            {
                //data = {"name":"jack","age":15}格式
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    
                //字符串转换为字节码
                byte[] bs = Encoding.UTF8.GetBytes(data);
                //参数类型,这里是json类型
                //还有别的类型如"application/x-www-form-urlencoded",不过我没用过(逃
                httpWebRequest.ContentType = "multipart/form-data;";
                //参数数据长度
                httpWebRequest.ContentLength = bs.Length;
                //设置请求类型
                httpWebRequest.Method = "POST";
                //设置超时时间
                httpWebRequest.Timeout = 20000;
                //将参数写入请求地址中
                httpWebRequest.GetRequestStream().Write(bs, 0, bs.Length);
    
                //发送请求
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                //读取返回数据
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                string responseContent = streamReader.ReadToEnd();
    
                streamReader.Close();
                httpWebResponse.Close();
                httpWebRequest.Abort();
    
                return responseContent;
            }
        }
    
    
    

      

     
  • 相关阅读:
    【题解】洛谷P1896 [SCOI2005] 互不侵犯(状压DP)
    [BZOJ4383][POI2015] Pustynia-[线段树+dp+拓扑排序]
    [agc016E]Poor Turkeys
    [arc082E]ConvexScore-[凸包]
    [BZOJ4011][HNOI2015]落忆枫音-[dp乱搞+拓扑排序]
    [arc062E]Building Cubes with AtCoDeer
    [arc079F]Namori Grundy
    [agc006F]Blackout
    [BZOJ4444][SCOI2015]国旗计划-[ST表]
    [BZOJ1007][HNOI2008]水平可见直线-[凸包]
  • 原文地址:https://www.cnblogs.com/xy0710/p/5807687.html
Copyright © 2011-2022 走看看