获得相同的响应
本文关键字:响应 | 更新日期: 2023-09-27 18:30:52
我是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时,一切正常。在第二次中,第一个请求工作正常,但第二个请求甚至不发送请求(我正在使用小提琴手),但说,他做到了。结果,我有第一个请求的结果。请帮帮我。
这有效:
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();
我在这里找到了它。