ウェブサイトがダウンした場合に備えて、リンクされた回答を参照してください。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Win8WinPhone.CodeShare.Extensions
{
public static class HttpExtensions
{
public static Task<Stream> GetRequestStreamAsync(this HttpWebRequest request)
{
var taskComplete = new TaskCompletionSource<Stream>();
request.BeginGetRequestStream(ar =>
{
Stream requestStream = request.EndGetRequestStream(ar);
taskComplete.TrySetResult(requestStream);
}, request);
return taskComplete.Task;
}
public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
{
var taskComplete = new TaskCompletionSource<HttpWebResponse>();
request.BeginGetResponse(asyncResponse =>
{
try
{
HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
taskComplete.TrySetResult(someResponse);
}
catch (WebException webExc)
{
HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
taskComplete.TrySetResult(failedResponse);
}
}, request);
return taskComplete.Task;
}
}
public static class HttpMethod
{
public static string Head { get{return "HEAD";} }
public static string Post { get{return "POST";} }
public static string Put { get{return "PUT";} }
public static string Get { get{return "GET";} }
public static string Delete { get{return "DELETE";} }
public static string Trace { get{return "TRACE";} }
public static string Options { get{return "OPTIONS";} }
public static string Connect { get{return "CONNECT";} }
public static string Patch { get{return "PATCH";} }
}
}
次に、関数を次のように記述できます。
public async Task<string> GetMyData(string urlToCall)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToCall);
request.Method = HttpMethod.Get;
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
そして、次のように呼び出します。
Tweet myTweet = await GetTweet(tweetID);
すべての功績は次のとおりです: http://www.windowsphonegeek.com/news/windows-8---windows-phone-code-sharing-httpwebrequest-getresponseasyncにリンクした @robertftw