我正在创建一个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";
我不知道如何将回复(Facebook联系人列表)作为JSON。
有人可以帮我吗?
答案 0 :(得分:2)
谢谢每一个人,我创造了方法,
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;
}
}
答案 1 :(得分:1)
使用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);