我正在创建一个Windows 8 Metro应用程序来获取Facebook联系人。在Windows 7中创建的普通Windows应用程序中,接受WebClient
,
private WebClient wc = new WebClient();
string jsonResponse = wc.DownloadString("https://graph.facebook.com/" + ID);
FacebookUser User = JsonConvert.DeserializeObject<FacebookUser>(jsonResponse);
但这在我的Windows 8 Metro App
中不起作用所以,我开始使用HTTPRequest
和HTTPResponse
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
我不知道如何获得JSON的响应(Facebook联系人列表)。
任何人可以帮我吗?
谢谢每个人,我创建了该方法,
public async void PerformHttpGet(string url,out string responseText)
{
int respCode = 0;
try
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpClient searchClient;
searchClient = new HttpClient();
searchClient.MaxResponseContentBufferSize = 256000;
HttpResponseMessage response = await searchClient.GetAsync(url);
response.EnsureSuccessStatusCode();
responseText = await response.Content.ReadAsStringAsync();
}
catch (WebException e)
{
string text = string.Empty;
string outRespType = string.Empty;
if (e.Response != null)
{
using (WebResponse response = e.Response)
{
outRespType = response.ContentType;
HttpWebResponse exceptionResponse = (HttpWebResponse)response;
respCode = (int)exceptionResponse.StatusCode;
using (System.IO.Stream data = response.GetResponseStream())
{
text = new System.IO.StreamReader(data).ReadToEnd();
};
};
}
throw e;
}
catch (Exception ex)
{
throw ex;
}
}
使用 System.Net.Http.HttpClient
:
HttpClient client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
HttpResponseMessage response = await client.GetAsync(address);
response.EnsureSuccessStatusCode();
String jsonResponse = await response.Content.ReadAsStringAsync();
FacebookUser User = JsonConvert.DeserializeObject<FacebookUser>(jsonResponse);