4 版本
HttpWebRequest httpReq; HttpWebResponse httpResp; string strHtml = ""; Uri httpURL = new Uri(Url); httpReq = (HttpWebRequest)WebRequest.Create(httpURL); httpReq.ProtocolVersion = HttpVersion.Version11; try { httpResp = (HttpWebResponse)httpReq.GetResponse(); StreamReader reader = new StreamReader(httpResp.GetResponseStream(), Encoding.UTF8); strHtml = reader.ReadToEnd(); } catch (WebException exception) { HttpWebRequest hReq; HttpWebResponse hResp; if (exception.Status == WebExceptionStatus.ConnectionClosed && exception.Response == null) { hReq = (HttpWebRequest)WebRequest.Create(httpURL); hReq.ProtocolVersion = HttpVersion.Version10; try { //hResp.ProtocolVersion = HttpVersion.Version10; hResp = (HttpWebResponse)hReq.GetResponse(); } catch (WebException exception1) { if (exception1.Status == WebExceptionStatus.ProtocolError && exception1.Response != null && exception1.Response is HttpWebResponse) { StreamReader reader = new StreamReader(((HttpWebResponse)exception1.Response).GetResponseStream(), Encoding.UTF8); strHtml = reader.ReadToEnd(); lstBoxInfo.Items.Add(((HttpWebResponse)exception1.Response).StatusCode); } } } else if (exception.Status == WebExceptionStatus.ProtocolError && exception.Response != null && exception.Response is HttpWebResponse) { StreamReader reader = new StreamReader(exception.Response.GetResponseStream(), Encoding.UTF8); strHtml = reader.ReadToEnd(); }
开始查了下用HttpVersion.Version11版本,异常中的respose为空,所以用10版本在异常捕获中,就可以得到。
但现在出现了 一个问题,在异常捕获中获得的reader是被截取的,最大65535,假如数据过大,就不是一个完整的html,丢数据。
求各位高手有什么解决办法,针对403 Forbidden的问题,关键浏览器都可以正常打开,而且查看到html都是正常完整的。
解决方案
60
不是你的问题。 本身就是403.
你去找找HttpWebResponse 获取错误后的页面。