2011-05-22 130 views
1

当使用HTTP Web请求时,当我尝试读取流时,我总是得到一个错误,说403禁止,但如果我尝试在VB.Net Web浏览器中执行它,它工作正常。这里是我的代码:HTTP Web请求中的VB.Net错误403

Imports System.Net 
Imports System.Text 
Imports System.IO 
Public Class Form2 
Dim logincookie As CookieContainer 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim postData As String = "test=test&username=test&password=test&next=test.html" 
    Dim tempCookies As New CookieContainer 
    Dim encoding As New UTF8Encoding 
    Dim byteData As Byte() = encoding.GetBytes(postData) 

    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://test.co.uk"), HttpWebRequest) 
    postReq.Host = "test.co.uk" 
    postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)" 
    postReq.Method = "POST" 
    postReq.KeepAlive = True 
    postReq.CookieContainer = tempCookies 
    postReq.ContentType = "application/x-www-form-urlencoded" 
    postReq.Referer = "http://test.co.uk" 
    postReq.ContentLength = byteData.Length 

    Dim postreqstream As Stream = postReq.GetRequestStream() 
    postreqstream.Write(byteData, 0, byteData.Length) 
    postreqstream.Close() 
    Dim postresponse As HttpWebResponse 

    postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse) 
    tempCookies.Add(postresponse.Cookies) 
    logincookie = tempCookies 
    Dim postreqreader As New StreamReader(postresponse.GetResponseStream()) 

    Dim thepage As String = postreqreader.ReadToEnd 

    MsgBox(thepage.ToString) 
End Sub 
End Class 

感谢, 亚当

回答

1

它与您的用户代理做。我面临同样的问题。尝试禁用或更改它,以便它符合您尝试读取的服务器要求。