简体   繁体   中英

getting empty response only in first http request Windows phone 8

Whenever I start an app in windows phone 8, and send http GET request I get empty response. This is just first request, responses from further requests to the same url are not empty.

Here is the code:

string res = "";

private void ReadUrlAsync(string url)
    {
        var request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;
       // request.Accept = "application/json;odata=verbose";
        request.BeginGetResponse(ResponseCallback, request);
    }

    private void ResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
        string data;
        using (Stream responseStream = response.GetResponseStream())
        {

            using (var reader = new System.IO.StreamReader(responseStream))
            {
                data = reader.ReadToEnd();
            }
        }
        res = data;
    }

I have used some different code and found solution:

        HttpClient cl = new HttpClient();
       return await cl.GetStringAsync(new Uri(url));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM