在Windows窗体中设置httpwebrequestcache策略

时间:2016-02-18 21:09:33

标签: c# .net

我正在尝试创建一个Window Form应用程序,该应用程序需要使用HttpWebRequest将预检和后检查令牌发送回服务器。我看过谷歌,我得到的所有答案都是针对ASP.NET应用程序的。我在MSDN(https://msdn.microsoft.com/en-us/library/system.net.webrequest.cachepolicy%28v=vs.110%29.aspx)上找到的一个例子似乎引导我朝着正确的方向前进,但当我拿出我的Header捕获工具时,我仍然看到缓存控制仍然设置为无缓存。我试过添加

request.Headers.Add("Cache-control", "public")

到我的代码,但我仍然在Fiddler中得到相同的错误我也尝试使用MSDN文章中的信息,但应用程序仍然没有缓存。如何让程序运行?

这是我的代码

            // create new webrequest object
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
            // reuse old headers 
            //request.Headers = response_headers;
            HttpRequestCachePolicy policy = new


            request.Referer = referalUrl;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers.Add("Accept-Encoding", "gzip,deflate");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
            request.CookieContainer = cookieJar;
            // no data to enter

            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // this is where I run into problems.

有什么想法吗?

0 个答案:

没有答案