当前位置: 代码迷 >> Windows Mobile >> HttpWebRequest 第一次请求 返回的都是正确的 但是后面的请求都是第一的解决方法
  详细解决方案

HttpWebRequest 第一次请求 返回的都是正确的 但是后面的请求都是第一的解决方法

热度:445   发布时间:2016-04-25 07:16:27.0
HttpWebRequest 第一次请求 返回的都是正确的 但是后面的请求都是第一的
private void HandleResponse(IAsyncResult asyncResult)
        {
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            httpRequest = (HttpWebRequest)asyncResult.AsyncState;
            httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(asyncResult);

            Stream streamResult = httpResponse.GetResponseStream();

            System.IO.StreamReader reader = new System.IO.StreamReader(streamResult, Encoding.UTF8);

            Deployment.Current.Dispatcher.BeginInvoke(() =>{Result = reader.ReadToEnd();});
        }


需要请求的地方实例化了的,但是在第二次请求的时候,老是返回第一次的值,难道是缓存?
弄不懂了,请大家帮我看看

------解决方案--------------------
是有缓存的,用如下方法可以每次刷新:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
//下面这行避免缓存
request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.Now.ToString();

//让你用上async
var response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);

给分吧,呵呵
------解决方案--------------------
HttpWebRequest默认是有缓存的,参考http://bbs.windever.com/thread-20092-1-1.html
------解决方案--------------------
HttpWebRequest请求发送前,设置下缓存策略试试

// Create the request.
WebRequest request = WebRequest.Create(uri);
// Define a cache policy for this request only. 
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;

------解决方案--------------------
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
//试试这个
request.Headers[HttpRequestHeader.CacheControl] = "no-cache";
------解决方案--------------------
这二个必须顶 刚好要这个
------解决方案--------------------
StreamReader读完后要Close,释放下资源。
  相关解决方案