当我收到网络异常时,我很难从vb.net中的HTTP网络请求中获取响应文本。
这是我正在使用的代码。
Try
myWebResponse = CType(request.GetResponse(), HttpWebResponse)
myStreamReader = New StreamReader(myWebResponse.GetResponseStream())
ResponseText = myStreamReader.ReadToEnd
If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then
SendResult = True 'Sent
SendStatus = 1 'message sent successfully
Try
Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU)
Catch ex As Exception
End Try
Else
SendStatus = 2 'message processed but not sent successfully
End If
Catch e As WebException
If (e.Status = WebExceptionStatus.ProtocolError) Then
Dim response As WebResponse = e.Response
Using (response)
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
myStreamReader = New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd & "Status Description = " & HttpWebResponse.StatusDescription
End Using
Catch ex As Exception
Logger.LogError(Me, ex)
End Try
End Using
令人讨厌的是,我正在联系的API使用404作为有效响应。如果我将请求放在浏览器中,将显示一些消息文本。我希望能够在我的程序中使用该文本。我不能简单地使用错误代码来确定操作,因为我认为我无法区分有效的404响应和实际错误。
在代码中这一行
myWebResponse = CType(request.GetResponse(), HttpWebResponse)
抛出异常。
在异常中,我可以获得404代码和描述,但不能获得响应流。它始终为空。
如果我得到200响应,我得到响应流中的文本没问题。
在Web异常响应对象(在Visual Studios调试器中)中,我检查了标头和对象值,但无法在任何地方找到响应文本。如果我在浏览器中粘贴请求URL,即使它是404,我也会得到响应文本。
小提琴手的原始回应:
HTTP/1.1 404 Not Found Connection: close Content-Type: text/plain; charset=UTF-8 Content-Length: 35 "The response Message"
关于如何在我的程序中获得“响应消息”的任何想法?我必须在服务器上使用.Net。
感谢任何人给予的任何帮助。
答案 0 :(得分:4)
此LINQPad查询工作正常,转储我的网络服务器的“未找到”错误网页提供的HTML:
Dim rq = System.Net.WebRequest.Create(New Uri("http://localhost/test"))
Try
Dim rs = rq.GetResponse
rs.Dump
Catch Ex As System.Net.WebException
Dim rs = Ex.Response
Call (New StreamReader(rs.GetResponseStream)).ReadToEnd.Dump
End Try
仅供参考我的代码适用于我,除了假定的拼写错误HttpWebResponse.StatusDescription
(以及评论出“不相关的东西”),再次作为LINQPad查询(在.NET 4.0中):
Dim request = WebRequest.Create("http://localhost/test")
Dim myStreamReader As StreamReader
Dim SendStatus As Integer = -1
Dim statusCode As HttpStatusCode
Dim ResponseText As String
Try
Dim myWebResponse = CType(request.GetResponse(), HttpWebResponse)
myStreamReader = New StreamReader(myWebResponse.GetResponseStream())
ResponseText = myStreamReader.ReadToEnd
If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then
'SendResult = True 'Sent
SendStatus = 1 'message sent successfully
'Try
' Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU)
'Catch ex As Exception
'End Try
Else
SendStatus = 2 'message processed but not sent successfully
End If
Catch e As WebException
If (e.Status = WebExceptionStatus.ProtocolError) Then
Dim response As WebResponse = e.Response
Using (response)
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
myStreamReader = New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd & "Status Description = " & httpResponse.StatusDescription ' HttpWebResponse.StatusDescription
End Using
Catch ex As Exception
'Logger.LogError(Me, ex)
ex.Dump("Exception")
End Try
End Using
End If
End Try
ResponseText.Dump("ResponseText")
我还确认上面的代码(添加了推断的As
子句并将.Dump
调用转换为Console.WriteLine
)在.NET 2.0中使用VB8。
答案 1 :(得分:0)
请注意,即使GetResponseStream()的行为抛出.NET WebException,HttpWebResponse实际上也会传递给WebException对象,因此在Catch中,您会在WebException上执行新的GetResponseStream()。响应对象。
下面,在初始GetResponseStream()的Catch中的非常相似的代码
Try
OriginalResponseStream = GetResponseStream(OriginalHTTPWebResponse)
Catch wex as WebException
Dim response As WebResponse = wex.Response
Dim statusCode As HttpStatusCode
Dim ResponseText As String
Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
statusCode = httpResponse.StatusCode
Try
Dim myStreamReader As New StreamReader(response.GetResponseStream())
Using (myStreamReader)
ResponseText = myStreamReader.ReadToEnd
Process(ResponseText) '<===as in whatever you need to do with the response
End Using
Catch ex As Exception
HandleIt(ex.Message) '<===as in whatever you want to do if Exception during the above
End Try
End Try