Http请求基本方法

Posted xiaoerduo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Http请求基本方法相关的知识,希望对你有一定的参考价值。

1.Http请求基本方法

/// <summary>
        /// Http请求基本方法
        /// </summary>
        /// <param name="content"></param>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="data"></param>
        /// <param name="referer"></param>
        /// <param name="contentTp"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public bool Request(ref string content, string url, string method, byte[] data, string referer = "", string contentTp = "", NameValueCollection headers = null)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method = method;
            request.Timeout = Timeout;
            request.AllowAutoRedirect = false;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60";
            request.Headers.Add("Accept-Language", "zh-TW,en;q=0.8,zh-CN;q=0.7,zh;q=0.5,zh-HK;q=0.3,en-US;q=0.2");
            request.Headers.Add("Accept-Encoding", "gzip,deflate");   /// gzip 如何判断并且解码 测试是否可以暂时先注释  https://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Headers.Add("Cache-Control", "max-age=0");
            request.KeepAlive = true;


            if (Proxy != null)
            {
                request.Proxy = Proxy;
            }

            if (!string.IsNullOrEmpty(referer))
            {
                request.Referer = referer;
            }

            if (headers != null && headers.Count > 0)
            {
                foreach (string key in headers.AllKeys)
                {
                    request.Headers.Add(key, headers[key]);
                }
            }

            if (cookieContainer == null)
            {
                cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
            }
            else
            {
                request.CookieContainer = cookieContainer;
            }

            if (method.ToLower() == "post")
            {
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Upgrade-Insecure-Requests", "1");
                request.ContentType = contentTp;

                if (data != null && data.Length > 0)
                {
                    request.ContentLength = data.Length;
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
            }
            else
            {
                request.Accept = "text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8";
            }

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (headers != null && headers.Count > 0)
                    {
                        foreach (string key in headers.AllKeys)
                        {
                            response.Headers.Add(key, headers[key]);
                        }
                    }

                    if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
                    {
                        if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
                        {
                            string redirectUrl = GetRedirectUrl(url, response.Headers["Location"]);
                            return Request(ref content, redirectUrl, "GET", null, url, contentTp);
                        }

                        Stream stream = response.GetResponseStream();
                        StreamReader streamReader = new StreamReader(stream, defaultEncoding);

                        content = streamReader.ReadToEnd();
                        streamReader.Close();
                        stream.Close();
                        response.Close();


                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (WebException e)
            {
                var res = (HttpWebResponse)e.Response;

                StreamReader sr = new StreamReader(res.GetResponseStream());

                var strHtml = sr.ReadToEnd();

                if (e.Response != null)
                {
                    using (Stream respStream = e.Response.GetResponseStream())
                    {
                        StreamReader streamReader = new StreamReader(respStream, Encoding.UTF8);
                        content = streamReader.ReadToEnd();
                        streamReader.Close();
                        respStream.Close();
                        e.Response.Close();
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                content = e.Message;
                return false;
            }
        }

  获取跳转url:

/// <summary>
        /// 跳转时,获取跳转URL
        /// </summary>
        /// <param name="url"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public static string GetRedirectUrl(string url, string location)
        {
            try
            {
                if (location.StartsWith("https://") || location.StartsWith("http://"))
                    return location;
                string newUrl = url;
                if (location.StartsWith("/"))
                {
                    newUrl = url.Substring(0, url.Substring(8).IndexOf("/") + 8);
                    return newUrl + location;
                }
                else if (location.StartsWith("../"))
                {
                    while (location.StartsWith("../"))
                    {
                        if (newUrl.IndexOf("/") > 0)
                        {
                            newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/"));
                        }
                        location = location.Substring(3);
                    }
                    return newUrl + "/" + location;
                }
                else if (location.StartsWith("./"))
                {
                    if (newUrl.IndexOf("/") > 0)
                    {
                        newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/"));
                    }
                    location = location.Substring(2);
                    return newUrl + "/" + location;
                }
                else
                {
                    if (newUrl.IndexOf("/") > 0)
                    {
                        newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/"));
                    }
                    return newUrl + "/" + location;
                }
            }
            catch
            {
                return url;
            }
        }

2.带文件的form提交

 /// <summary>
        /// 带文件的Form提交
        /// </summary>
        /// <param name="content"></param>
        /// <param name="url"></param>
        /// <param name="formData"></param>
        /// <param name="referer"></param>
        /// <returns></returns>
        public bool FileRequest(ref string content, string url, List<FormItem> formData, string referer, string lang = "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
        {
            try
            {
                string boundary = "---------------------------255132704032490"; //+ DateTime.Now.Ticks.ToString();

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.KeepAlive = true;
                request.CookieContainer = cookieContainer;
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method = "POST";
                request.Timeout = Timeout;
                request.AllowAutoRedirect = false;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
                request.Headers.Add("Accept-Language", lang);
                request.Headers.Add("Accept-Encoding", "deflate, br");
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Upgrade-Insecure-Requests", "1");


                if (!string.IsNullOrEmpty(referer))
                    request.Referer = referer;

                if (Proxy != null)
                {
                    request.Proxy = Proxy;
                }

                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                byte[] bdBytes = Encoding.UTF8.GetBytes("--" + boundary + "--
");
                byte[] brBytes = Encoding.UTF8.GetBytes("
");

                long length = bdBytes.Length;

                MemoryStream mStream = new MemoryStream();

                foreach (var item in formData)
                {
                    StringBuilder sb = new StringBuilder();

                    if (item.IsFile)
                    {

                        if (string.IsNullOrWhiteSpace(item.FilePath))
                        {
                            sb.Append($"--{boundary}
");
                            sb.Append($"Content-Disposition: form-data; name="{item.Name}"; filename="{item.FileName}"
");
                            sb.Append($"Content-Type: {item.ContentTp}");
                            sb.Append("
");
                            sb.Append("
");
                            sb.Append("
");
                        }
                        else
                        {
                            if (string.IsNullOrWhiteSpace(item.FileName))
                            {
                                sb.Append("--");
                                sb.Append(boundary);
                                sb.Append("
");
                                sb.Append("Content-Disposition: form-data; name="");
                                sb.Append(item.Name);
                                sb.Append(""; filename="");
                                sb.Append(System.IO.Path.GetFileName(item.FilePath));
                                sb.Append(""");
                                sb.Append("
");
                                sb.Append("Content-Type: ");
                                sb.Append(item.ContentTp);
                                sb.Append("
");
                                sb.Append("
");
                            }
                            else
                            {
                                sb.Append("--");
                                sb.Append(boundary);
                                sb.Append("
");
                                sb.Append("Content-Disposition: form-data; name="");
                                sb.Append(item.Name);
                                sb.Append(""; filename="");
                                sb.Append(item.FileName);
                                sb.Append(""");
                                sb.Append("
");
                                sb.Append("Content-Type: ");
                                sb.Append(item.ContentTp);
                                sb.Append("
");
                                sb.Append("
");
                            }
                        }
                    }
                    else
                    {
                        sb.Append("--");
                        sb.Append(boundary);
                        sb.Append("
");
                        sb.Append("Content-Disposition: form-data; name="");
                        sb.Append(item.Name);
                        sb.Append(""");
                        sb.Append("
");
                        sb.Append("
");
                        sb.Append(item.Value);
                        sb.Append("
");
                    }

                    string formTxt = sb.ToString();

                    byte[] data = Encoding.UTF8.GetBytes(formTxt);

                    mStream.Write(data, 0, data.Length);

                    length += data.Length;

                    if (item.IsFile)
                    {
                        if (!string.IsNullOrWhiteSpace(item.FilePath))
                        {
                            FileStream stream = new FileStream(item.FilePath, FileMode.Open, FileAccess.Read);

                            length += stream.Length;

                            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)stream.Length))];

                            int nRead = 0;

                            while ((nRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                mStream.Write(buffer, 0, nRead);
                            }

                            mStream.Write(brBytes, 0, brBytes.Length);

                            length += brBytes.Length;
                        }
                    }
                }

                /// 循环结束,需要增加结束标记 长度之前已经默认
                mStream.Write(bdBytes, 0, bdBytes.Length);
                mStream.Flush();

                request.ContentLength = length;

                long n = mStream.Length;

                using (Stream stream = request.GetRequestStream())
                {
                    /// 回头需要校验代码
                    byte[] data = mStream.GetBuffer();

                    stream.Write(data, 0, (int)mStream.Length);
                }
                HttpWebResponse res;
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        response.Headers.Add("Accept-Language", lang);
                        string redirectUrl = GetRedirectUrl(url, response.Headers["Location"]);

                        /// 判断是否跳转
                        if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
                        {
                            var headers = new NameValueCollection();
                            headers.Add("Accept-Language", lang);
                            return Get(ref content, redirectUrl, null, referer, headers);
                        }
                        else
                        {
                            Stream stream = response.GetResponseStream();
                            StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.UTF8);
                            content = streamReader.ReadToEnd();
                            streamReader.Close();
                            stream.Close();
                        }
                        response.Close();

                        return true;
                    }
                }
                catch (WebException ex)
                {
                    res = (HttpWebResponse)ex.Response;

                    StreamReader sr = new StreamReader(res.GetResponseStream());

                    var strHtml = sr.ReadToEnd();
                    //MainForm.Instance.Console("表单提交:" + e.Message);
                    return false;
                }


            }
            catch
            {
                //MainForm.Instance.Console("表单提交:" + e.Message);
                return false;
            }
        }

3.HTTP POST方式请求数据(带图片)

 /// <summary>
        /// HTTP POST方式请求数据(带图片)
        /// </summary>
        /// <param name="url">URL</param>        
        /// <param name="param">POST的数据</param>
        /// <param name="fileByte">图片Byte</param>
        /// <returns></returns>
        public string Post(string url, IDictionary<object, object> param, byte[] fileByte)
        {
            try
            {
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("
--" + boundary + "
");

                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
                wr.ContentType = "multipart/form-data; boundary=" + boundary;
                wr.UserAgent = "RK_C# 1.2";
                wr.Method = "POST";

                //wr.Timeout = 150000;
                //wr.KeepAlive = true;

                //wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
                Stream rs = null;
                try
                {
                    rs = wr.GetRequestStream();
                }
                catch { return "无法连接.请检查网络."; }
                string responseStr = null;

                string formdataTemplate = "Content-Disposition: form-data; name="{0}"

{1}";
                foreach (string key in param.Keys)
                {
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
                    string formitem = string.Format(formdataTemplate, key, param[key]);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                    rs.Write(formitembytes, 0, formitembytes.Length);
                }
                rs.Write(boundarybytes, 0, boundarybytes.Length);

                string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"
Content-Type: {2}

";
                string header = string.Format(headerTemplate, "image", "i.gif", "image/gif");//image/jpeg
                byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
                rs.Write(headerbytes, 0, headerbytes.Length);

                rs.Write(fileByte, 0, fileByte.Length);

                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("
--" + boundary + "--");
                rs.Write(trailer, 0, trailer.Length);
                rs.Close();

                WebResponse wresp = null;
                try
                {
                    wresp = wr.GetResponse();

                    Stream stream2 = wresp.GetResponseStream();
                    StreamReader reader2 = new StreamReader(stream2);
                    responseStr = reader2.ReadToEnd();

                    reader2.Close();
                    stream2.Close();
                    reader2.Dispose();
                    stream2.Dispose();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (wresp != null)
                    {
                        wresp.Close();
                        wresp = null;
                    }
                    wr.Abort();
                    wr = null;

                }
                return responseStr;
            }
            catch
            {
                //MainForm.Instance.Console("Ruokuai:" + e.Message);
                return "";
            }
        }

  

 

以上是关于Http请求基本方法的主要内容,如果未能解决你的问题,请参考以下文章

c# https请求忽略证书验证_各种编程语言忽略http的SSL证书认证

android webview不加载片段

postman 自动生成 curl 代码片段

postman 自动生成 curl 代码片段

Android中基于Http请求获取数据基础

Http请求基本方法