我最近遇到了一个我认为与CookieContainer有关的问题。
这是一个基本的HttpWebRequest到一个以前工作正常(2天前)的网址,但现在有问题。
以下是我正在使用的代码。
string url = "http://www.footywire.com/afl/footy/ft_match_statistics?mid=5827";
var cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieJar;
request.AllowAutoRedirect = false;
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate");
myWebHeaderCollection.Add("Accept-Language", "en-US,en;q=0.5");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
int cookieCount = cookieJar.Count;
response.Close();
我在GetResponse()调用期间收到(500)内部服务器错误。 该URL在浏览器中正常工作,但如果我使用隐身/私有窗口使我认为它必须与cookie相关,则会失败。
我已经使用fiddler监控请求/响应,并且可以看到请求根本不处理cookie。
以下是通过浏览器启用Cookie的工作请求。
GET http://www.footywire.com/afl/footy/ft_match_statistics?mid=5827 HTTP/1.1
Host: www.footywire.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __utma=123692316.616048912.1368166198.1400452319.1400457786.25; __gads=ID=c95b71c78d8a7516:T=1368166203:S=ALNI_MbHrLnKztzLrfUEkZNoQfcimetGNw; _em_vt=3938ffa289939f5bd80fd60798185181c3303dddd6-9262208553794a3e; __utmz=123692316.1395356585.4.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=123692316; JSESSIONID=62B598BD99DC2D228C4FC1E1549ED6FB; __utmb=123692316.1.10.1400457786; _em_v=5cc514d9ac92155cd445ac583ffd53794a3e7088d0-4657961653794a3e
Connection: keep-alive
这是代码发送的请求。明显缺少cookie。
GET http://www.footywire.com/afl/footy/ft_match_statistics?mid=5827 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Host: www.footywire.com
Connection: Keep-Alive
我有点不知道为什么它现在不工作,但它是2天前。我也不确定接下来该做什么。
任何帮助将不胜感激。感谢。