获得相同的回复

时间:2013-09-10 14:33:36

标签: c# windows-phone-7 asynchronous windows-phone-8 response

我是windows phone 8开发的新手。我试图异步获得响应。上次我使用this method来获得回复。在我的代码中,这看起来像这样:

 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时,一切正常。在第二次第一次请求工作正常,但第二次请求甚至没有发送请求(我正在使用提琴手),但他说,他做了。结果,我有第一次请求的结果。请帮帮我。

1 个答案:

答案 0 :(得分: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();

我发现它here