zoukankan      html  css  js  c++  java
  • C#HTTP请求之POST请求和GET请求

    POST请求

    /// <summary>
            /// POST请求获取信息
            /// </summary>
            /// <param name="url"></param>
            /// <param name="paramData"></param>
            /// <returns></returns>
            public string POST(string url, string paramData, int timeout = 5000, List<System.Net.Cookie> cookies = null)
            {
                string ret = string.Empty;
                try
                {
                    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(paramData); //转化
                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                    if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc;
                        webReq.ProtocolVersion = HttpVersion.Version10;
                    }
                    SetProxy(ref webReq);
                    webReq.Method = "POST";
                    webReq.ContentType = "application/json; charset=utf-8";
                    webReq.ServicePoint.Expect100Continue = false;
                    //webReq.ContentType = "text/html;charset=utf-8";
                    webReq.Timeout = timeout;
                    webReq.ContentLength = byteArray.Length;
    
                    if (!string.IsNullOrEmpty(UserAgent))
                        webReq.UserAgent = UserAgent;
                    if (cookies != null && cookies.Count > 0)
                    {
                        webReq.CookieContainer = new CookieContainer();
    
                        string host = new Uri(url).Host;
                        foreach (System.Net.Cookie c in cookies)
                        {
                            c.Domain = host;
                            webReq.CookieContainer.Add(c);
                        }
                    }
                    Stream newStream = webReq.GetRequestStream();
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                    newStream.Close();
                    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    ret = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    newStream.Close();
                }
                catch (Exception ex)
                {
                    ClassLoger.Error(ex, "HttpUtils/POST", url);
                    throw ex;
                }
                return ret;
            }
    View Code

    GET请求

            /// <summary>
            /// GET请求获取信息
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public string GET(string url, int timeout = 5000, List<System.Net.Cookie> cookies = null)
            {
                string ret = string.Empty;
                try
                {
                    HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url);
                    if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc;
                        web.ProtocolVersion = HttpVersion.Version10;
                    }
                    SetProxy(ref web);
                    web.Method = "GET";
                    web.Timeout = timeout;
                    if (!string.IsNullOrEmpty(UserAgent))
                        web.UserAgent = UserAgent;
                    if (cookies != null && cookies.Count > 0)
                    {
                        web.CookieContainer = new CookieContainer();
    
                        string host = new Uri(url).Host;
                        foreach (System.Net.Cookie c in cookies)
                        {
                            c.Domain = host;
                            web.CookieContainer.Add(c);
                        }
                    }
                    HttpWebResponse res = (HttpWebResponse)web.GetResponse();
                    Stream s = res.GetResponseStream();
                    StreamReader sr = new StreamReader(s, Encoding.UTF8);
                    ret = sr.ReadToEnd();
                    sr.Close();
                    res.Close();
                    s.Close();
                    //ClassLoger.Error("HttpUtils/GET",url+" "+ ret);
                }
                catch (Exception ex)
                {
                    ClassLoger.Error(ex, "HttpUtils/GET", url);
                    throw ex;
                }
                return ret;
            }
    View Code
  • 相关阅读:
    IB(InterBase Server) 的完整连接格式
    jna
    编写基于Prototype的Javascript动画类
    Go——使用 go mod ——有依赖的Go工程
    pkgconfig—— PKG_CONFIG_PATH——Makefile——pkgconfig的作用与使用
    Go——Goland Debug报错Version of Delve is too old for this version of Go
    NATS——NATS Streaming 是什么 (转)
    Go——Go语言调用C语言
    go get安装包超时处理
    NATS—基础介绍 (转自 https://www.cnblogs.com/yorkyang/p/8392172.html)
  • 原文地址:https://www.cnblogs.com/yonsy/p/7994917.html
Copyright © 2011-2022 走看看