Dato che ero solo supporto di Windows Phone 7.5 e superiori sono stato in grado di utilizzare questa libreria (Microsoft.Bcl.Async) per aggiungere async suppport alla mia plc e quindi utilizzare this solution.
Quindi il mio codice ha finito per assomigliare a questo:
public async Task<RequestResult> RunRequestAsync(string requestUrl, string requestMethod, object body = null)
{
HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest;
req.ContentType = "application/json";
req.Credentials = new System.Net.NetworkCredential(User, Password);
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", User, Password)));
var authHeader = string.Format("Basic {0}", auth);
req.Headers["Authorization"] = authHeader;
req.Method = requestMethod; //GET POST PUT DELETE
req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
if (body != null)
{
var json = JsonConvert.SerializeObject(body, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
byte[] formData = UTF8Encoding.UTF8.GetBytes(json);
var requestStream = Task.Factory.FromAsync(
req.BeginGetRequestStream,
asyncResult => req.EndGetRequestStream(asyncResult),
(object)null);
var dataStream = await requestStream.ContinueWith(t => t.Result.WriteAsync(formData, 0, formData.Length));
Task.WaitAll(dataStream);
}
Task<WebResponse> task = Task.Factory.FromAsync(
req.BeginGetResponse,
asyncResult => req.EndGetResponse(asyncResult),
(object)null);
return await task.ContinueWith(t =>
{
var httpWebResponse = t.Result as HttpWebResponse;
return new RequestResult
{
Content = ReadStreamFromResponse(httpWebResponse),
HttpStatusCode = httpWebResponse.StatusCode
};
});
}
private static string ReadStreamFromResponse(WebResponse response)
{
using (Stream responseStream = response.GetResponseStream())
using (StreamReader sr = new StreamReader(responseStream))
{
//Need to return this response
string strContent = sr.ReadToEnd();
return strContent;
}
}
E poi io chiamerei il codice con qualcosa di simile:
public async Task<bool> SampleRequest()
{
var res = RunRequestAsync("https//whatever.com/update/1", "PUT");
return await res.ContinueWith(x => x.Result.HttpStatusCode == HttpStatusCode.OK);
}
Se ciò non bastasse codice per voi non esitate a controlla il resto del progetto here
ho pensato che non era HttpClient in Windows Store quadro ancora e che stava arrivando con il rilascio di Windows blu questa estate? –