Mat*_*ton 2 .net httpwebresponse system.net.webexception asp.net-mvc-4 asp.net-web-api
我使用MVC4 Web API创建了一个RESTful Web服务.如果出现问题,我会抛出WebException.
throw new WebException("Account not found");
Run Code Online (Sandbox Code Playgroud)
这是处理异常的客户端代码:
private void webClientPost_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (e.Error == null)
{
// Display result
textBoxResult.Text = e.Result;
}
else
{
// Display status code
if (e.Error is WebException)
{
StringBuilder displayMessage = new StringBuilder();
WebException we = (WebException)e.Error;
HttpWebResponse webResponse = (System.Net.HttpWebResponse)we.Response;
displayMessage.AppendLine(webResponse.StatusDescription);
// Gets the stream associated with the response.
Stream receiveStream = webResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
displayMessage.Append(readStream.ReadToEnd());
readStream.Close();
textBoxResult.Text = displayMessage.ToString();
}
}
}
catch (Exception ex)
{
textBoxResult.Text = ex.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,客户端代码显示WebException中包含的HttpWebResponse.但是显示的是:
Internal Server Error {"Message":"An error has occurred."}
Run Code Online (Sandbox Code Playgroud)
这不是我的错误消息:-(所以我想我会使用Mircosoft MSDN WebException第4个构造函数指定的WebException的替代构造函数
在MSDN示例中,他们通过以下方式创建WebResponse:
MemoryStream memoryStream = new MemoryStream(recvBytes);
getStream = (Stream) memoryStream;
// Create a 'WebResponse' object
WebResponse myWebResponse = (WebResponse) new HttpConnect(getStream);
Exception myException = new Exception("File Not found");
// Throw the 'WebException' object with a message string, message status,InnerException and WebResponse
throw new WebException("The Requested page is not found.", myException, WebExceptionStatus.ProtocolError, myWebResponse);
Run Code Online (Sandbox Code Playgroud)
但是.Net Framework中不存在HttpConnect :-(.
有谁知道如何使用特定的WebResponse创建WebException?谢谢,马特
如果出现问题,我会抛出WebException
哦不,不.如果您的Web API出现问题,请设置相应的响应状态代码.
例如:
public HttpResponseMessage Get(int id)
{
var model = this.repository.Get(id);
if (model == null)
{
return Request.CreateErrorResponse(
HttpStatusCode.NotFound,
string.Format("Sorry but we couldn't find a resource with id={0}", id)
);
}
return Request.CreateResponse(HttpStatusCode.OK, model);
}
Run Code Online (Sandbox Code Playgroud)
然后在客户端:
using (var client = new WebClient())
{
try
{
string result = client.DownloadString("http://example.com/api/someresources/123");
}
catch (WebException ex)
{
// get the status code:
var response = (HttpWebResponse)ex.Response;
var statusCode = response.StatusCode;
// you could also read the response stream:
using (var reader = new StreamReader(response.GetResponseStream()))
{
// now you could read the body
string body = reader.ReadToEnd();
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5934 次 |
最近记录: |