2011-03-14 43 views
0

我有以下代碼:發送的HttpRequest背下來的HttpRequest(代理)

With context.Response 
    Dim req As HttpWebRequest = WebRequest.Create("http://www.Google.com/") 
    req.Proxy = Nothing 
    Dim res As HttpWebResponse = req.GetResponse() 
    Dim Stream As Stream = res.GetResponseStream 
    .OutputStream.Write(Stream, 0, Stream.Length) 
End With 

可悲的是,上面的代碼不起作用。我需要將RequestStream從context.Response中放入OutputStream中。

任何想法?

+0

你得到一個錯誤信息或者它不能編譯? – mdm 2011-03-14 09:07:24

+0

它不會編譯/錯誤消息。它不應該。 OutputStream.Write需要一個字節數組。我能做些什麼來讓我寫一個流到OutputStream? – FreeSnow 2011-03-14 09:27:07

回答

0

寫入需要一個字節數組,而您正在向它傳遞一個流。

嘗試從流中讀取並在寫回之前獲取所有數據。

首先,讀出的數據轉換成中間字節陣列(Taken from here):

Dim bytes(Stream.Length) As Byte 
Dim numBytesToRead As Integer = s.Length 
Dim numBytesRead As Integer = 0 
Dim n As Integer 
While numBytesToRead > 0 
    ' Read may return anything from 0 to 10. 
    n = Stream.Read(bytes, numBytesRead, 10) 
    ' The end of the file is reached. 
    If n = 0 Then 
     Exit While 
    End If 
    numBytesRead += n 
    numBytesToRead -= n 
End While 
Stream.Close() 

然後將其寫入到輸出流:

.OutputStream.Write(bytes, 0, Stream.Length)  
+0

謝謝,這解決了這個問題! – FreeSnow 2011-03-14 09:59:34