如何解决如何查看HTTPWebRequest响应?
|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 \'的值转换为\'String \'。
解决方法
s.GetResponse()返回WebResponse,这不是您只能打印的内容。实际的响应数据在result.Headers和result.GetResponseStream()中。您需要将其中的数据(您读取任何Stream的方式)读取到String中,然后才能将其输出到屏幕。
,听起来您正在创建基本代理。像Tridus所说的那样,您需要做的是获取响应流,然后将内容从一个写入另一个。之前,我曾在一个可能对您有所帮助的开源项目中这样做。
托管的Fusion Rewriter代理类
我知道这是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,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,bytesReturned)
End While
Catch exc As Exception
Manager.Log(\"Error on response: \" + exc.Message,\"Proxy\")
End Try
End Using
End Using
请注意,这是我的直接翻译,因此您将必须对自己的程序进行自定义。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。