获取httpwebrequest内容长度vb2005

发布于 2024-10-27 05:01:23 字数 1554 浏览 0 评论 0 原文

我有一个程序,用户可以输入带有 URL 编码查询字符串的完整 URL,然后将其发送到网络。

我在 vb2005 中使用 httpwebrequest

我从网站收到错误消息,指出

如果 URL 为 我应该发送内容长度。 php?q=somtext&param1=paramtext&param2=paramtext2" rel="nofollow">http://www.someurl.com/query.php?q=somtext&param1=paramtext&param2=paramtext2

如何我如何从 URL 获取内容长度,因为无法自动知道这一点?

编辑

我决定使用这个,这是正确的

Private Function GetHtmlFromUrl(ByVal url As String, _
                                   Optional ByVal PostData As String = vbNullString) As String

        If url.ToString() = vbNullString Then
            Throw New ArgumentNullException("url", "Parameter is null or empty")
        End If
        Dim html As String = vbNullString
        Dim myUrl As New System.Uri(url)
        Dim request As HttpWebRequest = WebRequest.Create(url)
        With request
            .ContentType = "Content-Type: application/x-www-form-urlencoded"
            .Method = "POST"
            .UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)"
            .Referer = "http://www.google.com"
            .ContentLength = myUrl.Query.Length
        End With



        Try
            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            html = Trim$(reader.ReadToEnd)
            Return html
        Catch ex As WebException
            Return ex.Message
        End Try

    End Function

I have a program where users input a complete URL with URL-encoded query string, and it send that to the web.

I am using the httpwebrequest in vb2005

I get an error from the websites saying that I should send a content length

if the URL is http://www.someurl.com/query.php?q=somtext¶m1=paramtext¶m2=paramtext2

how do I get the content length from a URL, as there is no way to know this automatically?

EDIT

i decided to use this, is this correct

Private Function GetHtmlFromUrl(ByVal url As String, _
                                   Optional ByVal PostData As String = vbNullString) As String

        If url.ToString() = vbNullString Then
            Throw New ArgumentNullException("url", "Parameter is null or empty")
        End If
        Dim html As String = vbNullString
        Dim myUrl As New System.Uri(url)
        Dim request As HttpWebRequest = WebRequest.Create(url)
        With request
            .ContentType = "Content-Type: application/x-www-form-urlencoded"
            .Method = "POST"
            .UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)"
            .Referer = "http://www.google.com"
            .ContentLength = myUrl.Query.Length
        End With



        Try
            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            html = Trim$(reader.ReadToEnd)
            Return html
        Catch ex As WebException
            Return ex.Message
        End Try

    End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕头说它不想醒 2024-11-03 05:01:23

问题是您指定方法“POST”,但将参数作为 URL 传递为“GET”。您要么需要使用 GET 方法,要么需要执行 POST(将参数写入请求流)。

http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

The problem is that you are specifying method "POST", but passing the arguments as the URL is a "GET". You either need to use the GET method or you need to perform a POST (write the parameters to the request stream).

http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文