【问题标题】:HttpWebRequest pass credentials to next HttpWebRequestHttpWebRequest 将凭据传递给下一个 HttpWebRequest
【发布时间】:2014-04-23 04:22:18
【问题描述】:

我正在使用 HttpWebRequest 登录页面并获取一些信息。然后我使用该信息创建一个新的 HttpWebRequest 以获取更多信息。我不想使用 WebClient。

如何将使用第一个 HttpWebRequest 登录获得的凭据传递给第二个?

编辑:如果我使用 CookieCollection 则返回为空。我只是尝试使用 WebClient 作为最后的手段,即使它不起作用,第二个请求也会让我回到登录屏幕。我注意到在 WebBrowser 中有一个 cookie。

【问题讨论】:

    标签: c# .net login httpwebrequest credentials


    【解决方案1】:

    在发送之前为每个请求添加CookieContainer。将您从第一个响应中获得的 cookie 添加到第二个请求。假设他们使用 cookie 进行身份验证,这应该对第二个请求进行身份验证。

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlWithParameters);
     request.CookieContainer = new CookieContainer();
    
     HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    
     var cookies = new CookieContainer();
     cookies.Add( response.Cookies );
    
     request = (HttpWebRequest)WebRequest.Create(secondUrlWithParameters);
     request.CookieContainer = cookies;
    
     ...
    

    【讨论】:

    • cookies.Add(response.Cookies);实际上也需要一个 Uri。我要为此传递什么。 cookies.Add(Uri?????, response.Cookies);
    • @anon - 应该有一个采用单个 CookieCollection 参数的方法:msdn.microsoft.com/en-us/library/6kyy4w9y.aspx。使用那个。
    • 原来 cookie 集合是空的。在这种情况下我该怎么办?
    • 太好了,我正在根据这个想法添加我的示例代码,我想它可以说明一些人。
    【解决方案2】:

    这只是基于答案 2 的示例运行代码。也许是多余的,也许可以说明某人。

                string url = "http://servername/place-where-data-is.extension"
                string loginUrl = "https://servername/sampleLogin?email=eeeeee&passwd=xxxxxxx";
    
    
                HttpWebRequest loginRequest = (HttpWebRequest)HttpWebRequest.Create(loginUrl);
                loginRequest.CookieContainer = new CookieContainer();
                loginRequest.Method = WebRequestMethods.Http.Get;
                HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
                var cookies = new CookieContainer();
                cookies.Add(loginResponse.Cookies);
    
    
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.CookieContainer = cookies;
                request.Method = WebRequestMethods.Http.Get;
    
                WebResponse response = (WebResponse)request.GetResponse();
    
                Stream responseStream = response.GetResponseStream();
    

    【讨论】:

      【解决方案3】:

      这是一个非常古老的问题,我知道它没有声明WebClient,但我会在这里为所有从谷歌遇到这个问题的人发帖。最初的概念不是我的代码。我不知道我最初是在哪里找到的。

      using (WebClientEx client = new WebClientEx())
      {
        client.IntTimeout = intTimeout;
        client.DownloadString(strReportUrlPrefix + strReportUrlQuery);
      
        NameValueCollection auth = new NameValueCollection
        {
          { "j_username", strReportUsername},
          { "j_password", strReportPassword}
        };
      
        byte[] data = client.UploadValues(strReportUrlPrefix + "j_security_check", auth);
      
        // LOGIC HERE WITH DATA
      }
      

      WebClientEx类:

      public class WebClientEx : WebClient
      {
        private CookieContainer _cookieContainer = new CookieContainer();
        public int IntTimeout { get; set; }
      
        protected override WebRequest GetWebRequest(Uri address)
        {
          WebRequest request = base.GetWebRequest(address);
          if (request != null)
            request.Timeout = IntTimeout;
      
          if (request is HttpWebRequest)
            (request as HttpWebRequest).CookieContainer = _cookieContainer;
      
          return request;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多