引发WebException如何获得与身体整体的反应? [英] WebException how to get whole response with a body?
本文介绍了引发WebException如何获得与身体整体的反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在引发WebException我看不到的GetResponse的身体。这是我的code在C#中:
{尝试
返回的GetResponse(。网址++ ext.ToString(),方法,标头,bodyParams);
}赶上(引发WebException前){
开关(ex.Status){
案例WebExceptionStatus.ConnectFailure:
抛出新ConnectionException();
案例WebExceptionStatus.Timeout:
抛出新RequestTimeRanOutException();
案例WebExceptionStatus.NameResolutionFailure:
抛出新ConnectionException();
案例WebExceptionStatus.ProtocolError:
如果(ex.Message ==远程服务器返回错误:(401)未经授权。){
抛出新CredentialsOrPortalException();
}
抛出新ProtocolErrorExecption();
默认:
扔;
}
我看到标题,但我没有看到尸体。这是Wireshark的输出要求:
POST /api/1.0/authentication.json HTTP / 1.1
内容类型:应用程序/ x-WWW的形式urlen codeD
接受:应用/ JSON
主持人:nbm21tm1.teamlab.com
内容长度:49
连接:保持活动的userName = XXX&放大器;密码= YYYHTTP / 1.1 500服务器错误
缓存控制:私人,最大年龄= 0
内容长度:106
内容类型:应用程序/ JSON的;字符集= UTF-8
服务器:Microsoft-IIS / 7.5
的X ASPNET-版本:2.0.50727
的X已启动方式:ASP.NET
的X已启动方式:ARR / 2.5日期:星期一,2012年8月6日12时49分41秒GMT
连接:关闭{数0的startIndex:0,身份:1,状态code:500,错误:{消息:无效的用户名或密码。}}
是否有可能以某种方式看到引发WebException消息文本?
谢谢你。
解决方案
VAR RESP =新的StreamReader(ex.Response.GetResponseStream())为ReadToEnd()。动态的obj = JsonConvert.DeserializeObject(RESP);
VAR messageFromServer = obj.error.message;
In WebException I cannot see body of GetResponse. This is my code in C#:
try {
return GetResponse(url + "." + ext.ToString(), method, headers, bodyParams);
} catch (WebException ex) {
switch (ex.Status) {
case WebExceptionStatus.ConnectFailure:
throw new ConnectionException();
case WebExceptionStatus.Timeout:
throw new RequestTimeRanOutException();
case WebExceptionStatus.NameResolutionFailure:
throw new ConnectionException();
case WebExceptionStatus.ProtocolError:
if (ex.Message == "The remote server returned an error: (401) unauthorized.") {
throw new CredentialsOrPortalException();
}
throw new ProtocolErrorExecption();
default:
throw;
}
I see header but I don't see body. This is output from Wireshark for the request:
POST /api/1.0/authentication.json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Host: nbm21tm1.teamlab.com
Content-Length: 49
Connection: Keep-Alive
userName=XXX&password=YYYHTTP/1.1 500 Server error
Cache-Control: private, max-age=0
Content-Length: 106
Content-Type: application/json; charset=UTF-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
Date: Mon, 06 Aug 2012 12:49:41 GMT
Connection: close
{"count":0,"startIndex":0,"status":1,"statusCode":500,"error":{"message":"Invalid username or password."}}
Is it possible somehow to see the message text in WebException? Thank you.
解决方案
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;
这篇关于引发WebException如何获得与身体整体的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文