分享一个我自己写的http/https通用的方法----支持代{过}{滤}理
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;
}
占个坑,观摩下 大佬分享,观摩学习! try....catch不需要指定异常吗 xiaolong23330 发表于 2021-11-11 09:28
try....catch不需要指定异常吗
图方便,哈哈,个人使用,其实没那么多讲究
页:
[1]