向Http请求发送Http请求(代理)

时间:2011-03-14 08:52:17

标签: vb.net stream httpwebrequest httpresponse

我有以下代码:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Write接受一个字节数组,而你传递一个流。

尝试从流中读取并在写回之前获取所有数据。

首先,将数据读入中间字节数组(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)