今天做模拟登陆的时候,发现HttpWebResponse的Cookie都为空,但是Fiddler看是有的。。。后来看见是302状态,才知道请求这个的时候,Response回来已经是跳转了。。。这样Cookie就会消失。

解决方法:

1. 

var request = (HttpWebRequest)WebRequest.Create(URL));

request.CookieContainer = new CookieContainer(); //这句大家都知道,如果想要获得response的cookie,这句必须写。
request.AllowAutoRedirect = false;

 

2. 如果用的是WebClient,就需要重写这个类:

 public class WebClientEx : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = (HttpWebRequest)base.GetWebRequest(address);
            request.AllowAutoRedirect = false;
            return request;
        }
    }

  var webClient = new WebClientEx();

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-07-28
  • 2021-08-20
  • 2021-08-01
  • 2022-12-23
猜你喜欢
  • 2022-01-02
  • 2021-11-04
  • 2021-11-16
  • 2021-07-05
  • 2021-04-29
  • 2022-12-23
相关资源
相似解决方案