关于 HttpWebResponse 返回forbidden的问题
我自己写的一个函数,用于获取页面的HTML代码,代码如下:
'==============================================================
Function ReadWebHTML(ByVal myUrl As String, ByVal myMethod As String, ByRef myCookie As Net.CookieContainer, Optional ByVal myPostData As String = " ") As String
Try
Dim HttpUrl As New Uri(myUrl & IIf(myPostData.Equals( " "), " ", "? " & myPostData))
Dim req As Net.HttpWebRequest
req = CType(Net.WebRequest.Create(HttpUrl), Net.HttpWebRequest)
req.Method = myMethod
req.ContentType = "application/x-www-form-urlencoded "
req.CookieContainer = myCookie
If myMethod = "POST " And myPostData.Equals( " ") = False Then
Dim bytesData() As Byte = System.Text.Encoding.ASCII.GetBytes(myPostData)
req.ContentLength = bytesData.Length
Dim postStream As IO.Stream = req.GetRequestStream()
postStream.Write(bytesData, 0, bytesData.Length)
End If
Dim res As Net.HttpWebResponse = CType(req.GetResponse(), Net.HttpWebResponse)
Dim reader As IO.StreamReader = New IO.StreamReader(res.GetResponseStream, System.Text.Encoding.GetEncoding( "GB2312 "))
Dim respHTML As String = reader.ReadToEnd() 'POST后网络服务器返回的信息
res.Cookies = myCookie.GetCookies(req.RequestUri)
reader.Close()
res.Close()
Return respHTML
Catch WebErr As Exception
Return ( "Error: " & WebErr.Message)
End Try
End Function
'==============================================================
通过测试,基本上都没有问题,但是读取某些页面就仅返回“Forbidden”(访问http://www.ylsa.cn/bbs下的页面都会有这个问题,但其他的都不会),请问有没有什么解决办法?
[解决办法]
try to fake an UserAgent:
Dim req As Net.HttpWebRequest
req = CType(Net.WebRequest.Create(HttpUrl), Net.HttpWebRequest)
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30) "