may sound like a noob, but this problem is really not getting out of my mind.I am facing weird problem regarding Windows Phone 8.1 . I developed an application for Windows Phone 8 that communicates to the Gateway server and process the requested data. We used HttpClient to send/receive data to Gateway server.However, Due to some reasons, I upgraded Windows Phone 8 OS to Windows Phone 8.1 but now, It doesn't process the request whenever I communicate to the Gateway server.
However, It works fine on Windows 8.1 Emulator But doesn't work, when you deploy application on actual Windows Phone 8.1 device (Previously upgraded from Windows Phone 8.0 os).
Moreover
var response = await RequestUtility.SendHttpRequest(myUri, httpMethod, stream, "text/xml", null, null, 0);
Response always contains 0 bytes whenever we run this application on Windows Phone 8.1.
Here is code snippet that communicate to the Server
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;
}
Request Utility code Snippet
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;
}
Any idea? Your ideas/suggestions would be appreciated.