我不得不手动连接我的网络层。要对Web服务运行查询,我使用以下代码:
public void RunService()
{
string serviceUrl = Constants.URL_OF_SERVICE;
WebRequest request = HttpWebRequest.Create(serviceUrl);
request.BeginGetResponse(new AsyncCallback(Service_Completed), request);
}
private void Service_Completed(IAsyncResult result)
{
try
{
WebRequest request = (WebRequest)(result.AsyncState);
WebResponse response = request.EndGetResponse(result);
// Continue doing stuff
}
catch (WebException ex1)
{
// How do I get the HTTP Status code here?
}
}
发生错误时,我在获取HTTP状态代码时遇到问题。有人可以告诉我如何在抛出WebException时获取HTTP状态代码吗?
谢谢!
答案 0 :(得分:3)
使用part of the WebException
的WebResponse
:
catch (WebException ex1)
{
using (HttpWebResponse response = (HttpWebResponse) ex1.Response)
{
if (response == null)
{
// Whatever
}
else
{
HttpStatusCode status = response.StatusCode;
// Whatever
}
}
}
答案 1 :(得分:1)
Windows Phone的问题是任何错误都被转换为“未找到404” - 即使根本没有网络或服务器根本没有响应,你仍然会得到“404” - 这是荒谬的,显然是错误。