111,120
社区成员
发帖
与我相关
我的任务
分享
public static RequestResult GetUrlHtml(string strUrl, string strRef, int nTimeOut, CookieContainer ckContainer) {
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebRespones = null;
string strHtml = string.Empty;
if (!Regex.IsMatch(strUrl, @"^https?://", RegexOptions.IgnoreCase)) strUrl = "http://" + strUrl;
httpWebRequest = WebRequest.Create(strUrl) as HttpWebRequest;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = nTimeOut;
httpWebRequest.ReadWriteTimeout = 60000;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)";
if (!string.IsNullOrEmpty(strRef)) httpWebRequest.Referer = strRef;
if (ckContainer != null) httpWebRequest.CookieContainer = ckContainer;
httpWebRespones = (HttpWebResponse)httpWebRequest.GetResponse();//timeout....
if (httpWebRespones.ContentType.ToLower().IndexOf("text/html") == -1) return new RequestResult(httpWebRequest, httpWebRespones, null);
using (Stream stream = httpWebRespones.GetResponseStream()) {
string strCharSet = Regex.Match(httpWebRespones.ContentType, @"charset=(\w+)", RegexOptions.IgnoreCase).Groups[1].Value.Trim();
List<byte> lst = new List<byte>();
int nRead = 0;
while ((nRead = stream.ReadByte()) != -1) lst.Add((byte)nRead);
byte[] byHtml = lst.ToArray();
if (strCharSet == "") {
strHtml = Encoding.UTF8.GetString(byHtml, 0, byHtml.Length);
strCharSet = Regex.Match(strHtml, @"<meta.*?charset=""?([a-z0-9-]+)\b", RegexOptions.IgnoreCase).Groups[1].Value;
if (strCharSet.ToLower().IndexOf("utf") == -1) {
try {
strHtml = Encoding.GetEncoding(strCharSet).GetString(byHtml, 0, byHtml.Length);
} catch { }
}
} else {
try {
strHtml = Encoding.GetEncoding(strCharSet).GetString(byHtml, 0, byHtml.Length);
} catch {
strHtml = Encoding.UTF8.GetString(byHtml, 0, byHtml.Length);
}
}
}
httpWebRespones.Close();
return new RequestResult(httpWebRequest, httpWebRespones, strHtml);
}
已经搞了一天了。不知道为什么 虽然想用socket直接去连接 但是想着要自己解析包 也是麻烦事情
再次强调 网络没有问题
所以如果说 告诉我 判断出现异常就再访问一次什么的就算了 就像楼上说的 有大于二分之一的情况都会出现timeout异常 都去尝试还得了 而且本来网络就没有问题
我想知道是什么情况导致的包都没有发出去 为什么5秒钟都没有发包 这5秒钟它都干嘛去了。。。