[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;
}
|