1

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?

4

2 回答 2

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;
        }
    }      
于 2012-11-01T08:40:54.927 回答
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);
于 2012-11-01T07:15:19.727 回答