2011-05-24 60 views
0
Dim s As HttpWebRequest 
Dim username= "username=" + HttpUtility.UrlEncode("username") 
Dim message = "message=" + HttpUtility.UrlEncode("message") 
Dim sep = "&" 
Dim sb As New StringBuilder() 
sb.Append(username).Append(sep).Append(message) 
s = HttpWebRequest.Create("http://www.website.com/?" + sb.ToString()) 
s.Method = "GET" 
Dim result = s.GetResponse()  

我該如何將結果寫入屏幕? 我得到錯誤類似'System.Net.WebResponse'類型的值無法轉換爲'字符串'。如何查看HTTPWebRequest響應?

回答

0

s.GetResponse()返回一個WebResponse,這不是您可以打印的東西。實際的響應數據在result.Headers和result.GetResponseStream()中。你需要從那裏讀取數據(你讀取任何流的方式)到一個字符串中,並且你可以輸出到屏幕上。

0

這聽起來像你正在創建一個基本代理。你需要做的就像Tridus說的那樣,得到響應流,然後將內容從一個寫到另一個。我之前在一個可以幫助你的開源項目中完成了這項工作。

Managed Fusion Rewriter Proxy Class

我知道這是C#,但過程還是在VB一樣。而應該是這個樣子:

Using responseStream = response.GetResponseStream() 
    Using bufferStream = New BufferedStream(responseStream, Manager.Configuration.Rewriter.Proxy.BufferSize) 
     Dim buffer As Byte() = New Byte(bufferSize - 1) {} 

     Try 
      While True 
       ' make sure that the stream can be read from 
       If Not bufferStream.CanRead Then 
        Exit While 
       End If 

       Dim bytesReturned As Integer = bufferStream.Read(buffer, 0, bufferSize) 

       ' if not bytes were returned the end of the stream has been reached 
       ' and the loop should exit 
       If bytesReturned = 0 Then 
        Exit While 
       End If 

       ' write bytes to the response 
       context.Response.OutputStream.Write(buffer, 0, bytesReturned) 
      End While 
     Catch exc As Exception 
      Manager.Log("Error on response: " + exc.Message, "Proxy") 
     End Try 
    End Using 
End Using 

請注意,這是從我的源直接翻譯,所以你將不得不進行自定義你自己的計劃。