吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2090|回复: 4
收起左侧

[其他转载] 分享一个我自己写的http/https通用的方法----支持代{过}{滤}理

  [复制链接]
MoEgg 发表于 2021-11-10 19:26
[C#] 纯文本查看 复制代码
 public string GetWebHtml(string url, string postData, bool isPost, Hashtable headers, string proxurl, string proxport, string proxAcc, string proxPwd, bool allowRedrect = true, bool putrequest = false, bool options = false, bool delete = false)
        {
            ServicePointManager.Expect100Continue = false;

            this.currentTry++;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;

            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(postData);
                httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.CookieContainer = this.cc;
                if (!string.IsNullOrEmpty(proxurl))
                {

                    WebProxy webP = new WebProxy(proxurl, int.Parse(proxport));
                    webP.Credentials = new NetworkCredential(proxAcc, proxPwd);//验证
                    httpWebRequest.UseDefaultCredentials = true;
                    httpWebRequest.Proxy = webP;
                }
                if (headers != null)
                {
                    foreach (DictionaryEntry de in headers)
                    {
                        httpWebRequest.Headers.Add(de.Key.ToString(), de.Value.ToString());
                    }
                }
                httpWebRequest.Accept = Accept;

                if (!string.IsNullOrEmpty(ContentType))
                    httpWebRequest.ContentType = ContentType;
                else
                    httpWebRequest.ContentType = null;

                httpWebRequest.AllowAutoRedirect = allowRedrect;
                httpWebRequest.UserAgent = this.UserAgent;
                httpWebRequest.ProtocolVersion = HttpVersion.Version11;httpWebRequest.Referer = this.Refer;
                httpWebRequest.Timeout = 31000;
                httpWebRequest.KeepAlive = true;
                if (!putrequest)
                    httpWebRequest.Method = (isPost ? "POST" : "GET");
                if (putrequest)
                    httpWebRequest.Method = "PUT";
                if (options)
                    httpWebRequest.Method = "OPTIONS";
                if (delete)
                    httpWebRequest.Method = "DELETE";
                httpWebRequest.ContentLength = (long)bytes.Length;
                httpWebRequest.ReadWriteTimeout = 31000;
                if (bytes.Length != 0)
                {
                    Stream requestStream = httpWebRequest.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }

                System.GC.Collect();

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                string text = string.Empty;
                bool flag = httpWebResponse.Headers["Content-Encoding"] != null && httpWebResponse.Headers["Content-Encoding"] == "gzip";
                if (flag)
                {
                    GZipStream gZipStream = new GZipStream(httpWebResponse.GetResponseStream(), CompressionMode.Decompress);
                    StreamReader streamReader = new StreamReader(gZipStream, encoding);
                    text = streamReader.ReadToEnd();
                    streamReader.Close();
                    gZipStream.Close();
                }
                else
                {
                    Stream responseStream = httpWebResponse.GetResponseStream();
                    StreamReader streamReader = new StreamReader(responseStream, this.encoding);
                    text = streamReader.ReadToEnd();
                    streamReader.Close();
                    responseStream.Close();
                }
                this.cc.Add(httpWebResponse.Cookies);

                string text2 = text;
                this.currentTry = 0;
                httpWebRequest.Abort();
                httpWebResponse.Close();
                result = text2;

            }
            catch (WebException webex)
            {
                string text = string.Empty;
                if (this.currentTry <= this.MaxTry)
                {
                    Thread.Sleep(3000);
                    text = this.GetWebHtml(url, postData, isPost, headers, proxurl, proxport, proxAcc, proxPwd, allowRedrect, putrequest, options);
                }
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                if (!string.IsNullOrEmpty(text))
                    result = text;
                else
                {
                    try
                    {
                        HttpWebResponse res = (HttpWebResponse)webex.Response;
                        Stream myResponseStream = res.GetResponseStream();
                        if (res.ContentEncoding != null && res.ContentEncoding.ToLower() == "gzip")
                        {
                            GZipStream gZipStream2 = new GZipStream(myResponseStream, CompressionMode.Decompress);
                            StreamReader streamReader = new StreamReader(gZipStream2, this.encoding);
                            text = streamReader.ReadToEnd();
                            result = text;
                        }
                        else
                        {
                            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                            string retString = myStreamReader.ReadToEnd();
                            result = retString;
                        }
                    }
                    catch
                    {
                        result = "重试后无网络。";
                    }

                }
            }
            finally
            {
                ContentType = string.Empty;
            }
            return result;
        }

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

不知道改成啥 发表于 2021-11-10 23:18
占个坑,观摩下
yhgfwly007 发表于 2021-11-11 09:11
xiaolong23330 发表于 2021-11-11 09:28
 楼主| MoEgg 发表于 2021-11-11 21:04
xiaolong23330 发表于 2021-11-11 09:28
try....catch不需要指定异常吗

图方便,哈哈,个人使用,其实没那么多讲究
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 11:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表