Ошибка VB.Net 403 в веб-запросах HTTP - PullRequest
1 голос
/ 22 мая 2011

при использовании веб-запросов HTTP, когда я пытаюсь прочитать поток, я всегда получаю сообщение об ошибке 403 «Запрещено», но если я пытаюсь сделать это в веб-браузере 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

Спасибо, Адам

1 Ответ

1 голос
/ 25 ноября 2011

Это связано с вашим useragent.Я сталкиваюсь с той же проблемой.Попробуйте отключить или изменить его так, чтобы он соответствовал требованиям сервера, с которого вы пытаетесь читать.

...