Net错误403在HTTP的Web请求

Net错误403在HTTP的Web请求

本文介绍了VB.Net错误403在HTTP的Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

while using HTTP web Requests, when i try and read the stream i always get an error saying 403 Forbidden but if i try to do it in the VB.Net web browser it works fine. Here is my code:

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

谢谢,
亚当

Thanks,Adam

推荐答案

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

It has to do with your useragent. I am facing the same problem. Try disabling it or change it so it meets the server requirments your trying to read from.

这篇关于VB.Net错误403在HTTP的Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:05