2010-02-23 45 views
0

有沒有另一種方式很容易在.NET中發出一個POST請求而不是WebRequest類?我有一個非常,非常小的數據塊我需要張貼:在VB.NET中發佈小數據

密碼= theword

...但WebRequest的隨機,我的意思是隨機,張貼到我的服務器時丟棄數據。我已經通過使用我的服務器中的一段代碼來測試它,該代碼將請求轉儲到控制檯,並且我可以確定客戶端有時發送,有時不發送POST數據。

我正在使用的使用WebRequest的代碼在與IIS交談時在另一個項目中工作。每當我通過Firefox向其發送數據時,正在與之通話的服務器(另一個系統中的最小Web服務器)都會正確響應。我在同一個項目中有一個函數可以觸發一個GET請求,並且工作正常。這看起來像我的POST函數沒有完成交易...過去我注意到要求WebRequest處理小字符串的事情。

下面是讓我適合的代碼。如果有任何.NET專家可以指出我的錯誤或建議另一個Web客戶端,我會非常感激。謝謝!

Private Function PostRequest(ByVal url As String, ByVal data As String) As String 
    Return ControlFunctions.PostRequest(url, data, 0) 
End Function 

Private Function PostRequest(ByVal url As String, ByVal data As String, ByVal times As Integer) As String 
    Dim req As HttpWebRequest = WebRequest.Create(url) 
    Dim retval As String = "" 

    req.Method = "POST" 
    req.UserAgent = "TSControl" 
    req.ContentType = "application/x-www-form-urlencoded" 
    req.ContentLength = data.Length 
    req.Headers.Add("Keep-Alive", "300") 
    req.KeepAlive = True 
    req.Timeout = 5000 

    Try 
     Dim DataStream As StreamWriter = New StreamWriter(req.GetRequestStream()) 
     DataStream.AutoFlush = True 
     DataStream.Write(data) 
     DataStream.Close() 

     Dim sr As StreamReader = New StreamReader(req.GetResponse().GetResponseStream()) 
     retval = sr.ReadToEnd() 
     sr.Close() 
    Catch x As Exception 
     If times < 5 Then 
      Threading.Thread.Sleep(1000) 
      times = times + 1 
      ControlFunctions.PostRequest(url, data, times) 
     Else 
      ErrorMsg.Show("Could not post to server" + vbCrLf + x.Message + vbCrLf + x.StackTrace) 
     End If 
    End Try 

    Return retval 
End Function 

----更新---

我不得不去下修復它,但幸運的是我會使用.NET的套接字庫涉足過去時代:

Private Function PostRequest(ByVal url As String, ByVal data As String) As String 
    Dim uri As New Uri(url) 
    Dim read(16) As Byte 
    Dim FullTime As New StringBuilder 
    Dim PostReq As New StringBuilder 
    Dim WebConn As New TcpClient 

    PostReq.Append("POST ").Append(uri.PathAndQuery).Append(" HTTP/1.1").Append(vbCrLf) 
    PostReq.Append("User-Agent: TSControl").Append(vbCrLf) 
    PostReq.Append("Content-Type: application/x-www-form-urlencoded").Append(vbCrLf) 
    PostReq.Append("Content-Length: ").Append(data.Length.ToString).Append(vbCrLf) 
    PostReq.Append("Host: ").Append(uri.Host).Append(vbCrLf).Append(vbCrLf) 
    PostReq.Append(data) 

    WebConn.Connect(uri.Host, uri.Port) 
    Dim WebStream As NetworkStream = WebConn.GetStream() 
    Dim WebWrite As New StreamWriter(WebStream) 

    WebWrite.Write(PostReq.ToString) 
    WebWrite.Flush() 

    Dim bytes As Integer = WebStream.Read(read, 0, read.Length) 

    While bytes > 0 
     FullTime.Append(Encoding.UTF8.GetString(read)) 
     read.Clear(read, 0, read.Length) 
     bytes = WebStream.Read(read, 0, read.Length) 
    End While 

    ' Closes all the connections 
    WebWrite.Close() 
    WebStream.Close() 
    WebConn.Close() 

    Dim temp As String = FullTime.ToString() 

    If Not temp.Length <= 0 Then 
     Return temp 
    Else 
     Return "No page" 
    End If 
End Function 

回答

0

當你說「.net刪除數據」時,你的意思是什麼?

,如果你想要做的是發佈namevalue對,你可以如下做到這一點:

WebClient client = new WebClient(); 
NameValueCollection nv = new NameValueCollection(); 
nv.Add("password", "theword"); 
client.UploadValues(url, "POST", nv); 

請注意,你可能要檢查的傳遞給UploadValues參數的順序,我不知道這個順序是對的,而且現在懶得去查MSDN。

2

如果您收到「chunked」迴應怎麼辦?你如何解碼它?

這就是我一直在努力今天整天。如果我正在處理普通的Web服務,那麼我不會掙扎太多,但是我正在製作一個讀取eBay的API數據的應用程序,而eBay有時會發送錯誤的塊大小,這會使解碼器破壞響應(即使您的解碼器通過HTTP 1.1規則進行解碼)。在嘗試解碼eBay的BAD API好幾個小時後,我最終創建了一個解碼VB.net中的分塊http響應的函數,並且可以使用衆多在線工具之一將其輕鬆轉換爲C#。首先,你檢查HTTP響應頭,如果響應內容類型分塊,如果是這樣,只是通過針對這種功能,它通過引用,採用的變量和操縱它的身體:

Public Shared Sub DechunkString(ByRef strString As String) 
     Dim intChunkSize As Integer = 0 
     Dim strSeperator(0) As String 
     strSeperator(0) = vbCrLf 

     Dim strChunks As String() = strString.Split(strSeperator, StringSplitOptions.RemoveEmptyEntries) 

     strString = "" 

     For Each strChunk As String In strChunks 
      If strChunk.Length = intChunkSize Then ' for normal behaviour this would be enough 
       strString &= strChunk 
       intChunkSize = 0 
       Continue For 
      End If 

      ' because of sometimes wrong chunk sizes let's check if the next chunk size is a valid hex, and if not, treat it as part chunk 
      If strChunk.Length <= 4 Then ' this is probably a valid hex, but could have invalid characters 
       Try 
        intChunkSize = CInt("&H" & strChunk.Trim()) 

        If intChunkSize = 0 Then 
         Exit For 
        End If 

        Continue For 
       Catch ex As Exception 

       End Try 
      End If 

      ' if got to this point, then add the chunk to output and reset chunk size 
      strString &= strChunk 
      intChunkSize = 0 
     Next 
    End Sub 

我只希望它幫助某人節省大量的時間和精力。