• 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
  • 相关阅读:
    请求转发和重定向
    jvm调优(新生代、老年代调优)
    servlet(对servlet的理解、生命周期)
    http的get和post请求方式
    jvm垃圾回收器(串行、吞吐量优先、响应时间优先、G1)
    java后端学习路线总结
    jvm:java中的引用(强引用、软引用、虚引用、弱引用)
    jvm直接内存(分配与回收)
    ssm(增删改查、拦截器、过滤器)
    springmvc:文件的上传与下载
  • 原文地址:https://www.cnblogs.com/yonsy/p/7994917.html
Copyright © 2020-2023  润新知