我正在使用c#/ xaml开发一个Windows Phone 8应用程序。我正面临着 httpwebrequest 类的一些问题。我想使用post方法从服务器下载数据。但是httpwebrequest没有按预期工作。当我尝试随后调用webservices时,它返回错误(远程服务器返回错误:NotFound)。可能是什么原因?。请帮忙......以下是我的代码。
string response = "";
httpwebrequest = WebRequest.Create(new Uri(serviceurl)) as HttpWebRequest;
httpwebrequest.Method = "POST";
httpwebrequest.ContentType = "application/json";
byte[] data = Serialization.SerializeData(request);
using (var requestStream = await Task<Stream>.Factory.FromAsync(httpwebrequest.BeginGetRequestStream, httpwebrequest.EndGetRequestStream, null))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
response = await httpRequest(httpwebrequest);
var result = Serialization.Deserialize<T>(response);
return result;
}
public async Task<string> httpRequest(HttpWebRequest request)
{
try
{
string received;
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
received = await sr.ReadToEndAsync();
}
}
response.Close();
}
return received;
}
catch(Exception ex)
{
return "";
}
}
答案 0 :(得分:0)
您检查过内部异常吗?因为我有一个类似的问题,当我检查内部异常时,我发现我从服务器得到了另一个错误。还要检查响应标头中的真实http状态代码。