0

我是 windows phone 8 开发的新手。我正在尝试异步获得响应。上次我使用这种方法来获得响应。在我的代码中,它看起来像这样:

 public async void StartButton_Click(object sender, RoutedEventArgs e)
    {
        cc = new CookieContainer();
        String url = "http://???/play/multi";
        String sss = await createHttpRequestMulti(url); //1st
        url = "http://???/play/check";
        sss = await createHttpRequestMulti(url); //2nd
        while (!sss.StartsWith("["))
            sss = await createHttpRequestMulti(url);
    }
   private async Task<string> createHttpRequestMulti(string url)
    {
        var myHttpRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
        myHttpRequest.Method = "GET";
        myHttpRequest.AllowAutoRedirect = true;
        myHttpRequest.CookieContainer = cc;
        var response = (HttpWebResponse)await myHttpRequest.GetResponseAsync();
        var reader = new StreamReader(response.GetResponseStream());
        string responseString = await reader.ReadToEndAsync();
        response.Close();
        reader.Close();
        return responseString;
    }

GetResponseAsync 看起来像:

        public static Task<WebResponse> GetResponseAsync(this WebRequest request)
    {
        return Task.Factory.FromAsync<WebResponse>(
            request.BeginGetResponse, request.EndGetResponse, null);
    }

仅在我第一次使用 StartButton_Click 时一切正常。在第二次第一个请求工作正常,但第二个请求甚至不发送请求(我正在使用提琴手),但说,他做到了。结果,我得到了第一个请求的结果。请帮帮我。

4

1 回答 1

0

这有效:

url + "?param=" + RND.Next(10000)

但我更喜欢:

    HttpWebRequest request = HttpWebRequest.CreateHttp(url);
    if (request.Headers == null)
    {
        request.Headers = new WebHeaderCollection();
    }
    request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();

我在这里找到了。

于 2013-09-11T06:41:20.830 回答