2011-03-25 72 views
0

我有一個程序,用戶輸入一個帶有URL編碼查詢字符串的完整URL,然後將其發送到Web。獲取httpwebrequest內容長度vb2005

我使用VB2005的HttpWebRequest的

我從網站上的錯誤,說I should send a content length

如果URL是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 

回答

1

的問題是,您指定方法「POST」,但傳遞的參數作爲URL是「GET 」。您可能需要使用GET方法,或者需要執行POST(將參數寫入請求流)。

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

+0

我不認爲代碼是與我的不同,礦給人'錯誤error'同樣的事情發生在我使用該代碼 – Smith 2011-03-25 16:41:53

+0

@Smith 500 INTERNAT服務器 - 您的代碼不包含任何書面形式請求流。您的代碼正在執行GET請求,但指出它是POST請求。看看我更仔細地鏈接的文章,但首先你需要驗證有問題的網站想要看到哪種方法(GET或POST)。 – Tergiver 2011-03-25 20:55:52