【问题标题】:Get httpwebrequest content-length vb2005获取httpwebrequest内容长度vb2005
【发布时间】:2011-03-25 11:19:00
【问题描述】:

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

我在vb2005中使用httpwebrequest

我从网站上收到一条错误消息,说 I should send a content length

如果网址是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

【问题讨论】:

    标签: .net winforms httpwebrequest http-content-length


    【解决方案1】:

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

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

    【讨论】:

    • 我不认为该代码与我的不同,我的给error 500 internat server error 当我使用该代码时会发生同样的事情
    • @Smith - 您的代码不包含对请求流的任何写入。您的代码正在执行 GET 请求,但声明它是 POST 请求。查看我链接的文章更仔细,但首先您需要验证相关网站希望查看哪种方法(GET 或 POST)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多