I'm creating a Windows 8 Metro App to fetch Facebook contacts. In a normal windows application created in Windows 7 accepts WebClient
,
private WebClient wc = new WebClient();
string jsonResponse = wc.DownloadString("https://graph.facebook.com/" + ID);
FacebookUser User = JsonConvert.DeserializeObject<FacebookUser>(jsonResponse);
but this does not work in my Windows 8 Metro App
So, I started using HTTPRequest
and HTTPResponse
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
I don't know how to get the response (list of Facebook contacts) as a JSON.
Can anyone help me please?
Thanks Every one, I Created the Method,
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;
}
}
Use 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.