0

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&param1=paramtext&param2=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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Smith
  • 5,765
  • 17
  • 102
  • 161

1 Answers1

1

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

Tergiver
  • 14,171
  • 3
  • 41
  • 68
  • I don't think that code is different from mine, Mine gives `error 500 internat server error` the same thing happens when i use that code – Smith Mar 25 '11 at 16:41
  • @Smith - Your code does not contain any writing to the request stream. Your code is performing a GET request, but stating that it is a POST request. Look at the article I linked more carefully, but first you need to verify which method (GET or POST) the website in question wants to see. – Tergiver Mar 25 '11 at 20:55