【问题标题】:VB.Net Error 403 in HTTP Web RequestsHTTP Web 请求中的 VB.Net 错误 403
【发布时间】:2011-11-25 17:29:16
【问题描述】:

在使用 HTTP 网络请求时,当我尝试读取流时,我总是收到一条错误消息,提示 403 Forbidden,但如果我尝试在 VB.Net 网络浏览器中执行此操作,它工作正常。这是我的代码:

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

谢谢, 亚当

【问题讨论】:

    标签: vb.net post login httpwebrequest browser


    【解决方案1】:

    这与您的用户代理有关。我面临同样的问题。尝试禁用或更改它,使其满足您尝试读取的服务器要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多