2011-03-14 45 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