[英]How I refresh a HttpWebRequest object on Windows Phone
I'm writing a windows phone application where I need to do a http request. 我正在编写Windows Phone应用程序,我需要执行http请求。 This request return to me a random value it changes always when I request its url.
此请求返回给我一个随机值,当我请求其url时该值始终会更改。
So I have a app with a button when I click it I receive the random value that I want, but, if I click the button again I receive the same random value!! 因此,我有一个带有按钮的应用程序,当我单击它时,我会收到想要的随机值,但是,如果再次单击该按钮,我将收到相同的随机值! I put a breakpoint on the server side and it is reached just once, if a click the button 10 times it will reach the breakpoint just the first time!!
我在服务器端设置了一个断点,只有一个断点,如果单击10次按钮,它将是第一次断点! I need whenever I click the button I have a new random value!
每当我单击按钮时,我都需要一个新的随机值!
And I need to use a CookieContainer because on my server I need to keep some objects defined in this session. 我需要使用CookieContainer,因为在我的服务器上,我需要保留一些在此会话中定义的对象。 If I dont use CookieContainer I cant keep the session, so when I call the second request I haven't no objects there.
如果我不使用CookieContainer,则无法保持会话,因此当我调用第二个请求时,那里没有对象。
private async Task<string> requestURLData(HttpWebRequest request)
{
HttpWebResponse response = (HttpWebResponse) await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = response.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}
private async Task<string> requestUrl(string url, CookieContainer cookie)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.CookieContainer = cookie;
return await this.requestURLData(request);
}
private async Button_Click(object sender, RoutedEventArgs e)
{
CookieContainer cookie = new CookieContainer();
var result = await this.requestUrl(this.urlBase + "getRandomNum", cookie);
result = await this.requestUrl(this.urlBase + "getAnotherValue", cookie);
}
You're running into response caching - append a random string to the end of your url that your server ignores to guarantee that you don't hit the cache: 您正在遇到响应缓存-在服务器忽略的URL末尾附加一个随机字符串,以确保您不会访问缓存:
Random _rand = new Random();
private async Button_Click(object sender, RoutedEventArgs e)
{
CookieContainer cookie = new CookieContainer();
var result = await this.requestUrl(this.urlBase + "getRandomNum&rubbish=" + _rand.Next(), cookie);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.