【发布时间】:2014-09-08 07:25:38
【问题描述】:
可能听起来像个菜鸟,但这个问题真的没有让我忘记。我正面临关于 Windows Phone 8.1 的奇怪问题。我为 Windows Phone 8 开发了一个应用程序,它与网关服务器通信并处理请求的数据。我们使用 HttpClient 向网关服务器发送/接收数据。但是,由于某些原因,我将 Windows Phone 8 操作系统升级到了 Windows Phone 8.1,但现在,每当我与网关服务器通信时,它都不会处理请求。
但是,它在 Windows 8.1 模拟器上运行良好,但在实际的 Windows Phone 8.1 设备(之前从 Windows Phone 8.0 操作系统升级)上部署应用程序时不起作用。
此外var response = await RequestUtility.SendHttpRequest(myUri, httpMethod, stream, "text/xml", null, null, 0); 每当我们在 Windows Phone 8.1 上运行此应用程序时,响应总是包含 0 个字节。
这是与服务器通信的代码 sn-p
public async Task<bool> ValidateUserRequest(User user)
{
var result = false;
var myUri = new Uri(GatewayUrl);
var content = RequestUtility.ValidateRequestBuilder(user);
try
{
var httpMethod = new HttpMethod("POST");
MemoryStream stream = new MemoryStream((new UTF8Encoding()).GetBytes(content.ToString(CultureInfo.InvariantCulture)));
var response = await RequestUtility.SendHttpRequest(myUri, httpMethod, stream, "text/xml", null, null, 0);
string ecodedString = Encoding.UTF8.GetString(response, 0, response.Length);
var serializer = new XmlSerializer(typeof(ResponseType));
ResponseType responseObject;
using (var reader = new StringReader(ecodedString))
{
responseObject = serializer.Deserialize(reader) as ResponseType;
}
if (responseObject != null) result = responseObject.Approved;
}
catch
{
result = false;
}
return result;
}
请求实用程序代码片段
public static async Task<byte[]> SendHttpRequest(Uri uri, HttpMethod method, Stream requestContent = null, string contentType = null, HttpClientHandler clientHandler = null, Dictionary<string, string> headers = null, int timeoutInSeconds = 0)
{
var req = clientHandler == null ? new HttpClient() : new HttpClient(clientHandler);
var message = new HttpRequestMessage(method, uri);
byte[] response;
if (requestContent != null && (method == HttpMethod.Post || method == HttpMethod.Put || method == HttpMethod.Delete))
{
message.Content = new StreamContent(requestContent); //set the body for the request
if (!string.IsNullOrEmpty(contentType))
{
message.Content.Headers.Add("Content-Type", contentType); // if the request has a body set the MIME type
}
}
// append additional headers to the request
if (headers != null)
{
foreach (var header in headers)
{
if (message.Headers.Contains(header.Key))
{
message.Headers.Remove(header.Key);
}
message.Headers.Add(header.Key, header.Value);
}
}
// Send the request and read the response as an array of bytes
if (timeoutInSeconds > 0)
{
req.Timeout = new TimeSpan(0, 0, timeoutInSeconds);
}
using (var res = await req.SendAsync(message))
{
response = await res.Content.ReadAsByteArrayAsync();
}
return response;
}
有什么想法吗?您的想法/建议将不胜感激。
【问题讨论】:
-
如果它在模拟器中运行但在设备中运行,我会检查看看可能有什么不同。手机升级正常吗?它是否遇到任何其他连接问题?您是否尝试过创建一个新项目并测试一个简单的 HttpRequest?
-
我也有同样的问题,我的代码在 Windows 8.1 上运行良好,但在 Windows Phone 8.1 上运行良好(响应内容为空)> 你解决了这个问题吗?
标签: c# windows-phone-8 windows-phone-8.1 windows-phone-8-emulator