将 HttpRequest 发送回 HttpRequest(代理)

发布于 2024-10-21 23:14:20 字数 426 浏览 1 评论 0原文

我有以下代码:

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 中。

有什么想法吗?

I have the following code:

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

Sadly, the above code doesn't work. I need to take the RequestStream and put it into the OutputStream from the context.Response.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2024-10-28 23:14:20

Write 接受一个字节数组,而您向它传递一个流。

尝试从流中读取并获取所有数据,然后再将其写回。

首先,将数据读入中间字节数组(取自这里):

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)    

Write takes a byte array, whereas you are passing it a stream.

Try reading from the stream and getting all the data before writing it back.

First, read the data into an intermediate byte array (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()

Then write it to the output stream:

.OutputStream.Write(bytes,  0, Stream.Length)    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文