HttpWebRequest pass credentials to next HttpWebRequest

女生的网名这么多〃 提交于 2019-11-30 07:40:56

问题


I am logging into a page using HttpWebRequest and getting some information. I then use that information to create a new HttpWebRequest to get some more information. I do not want to use WebClient.

How can I pass the credentials I obtained from logging in using the first HttpWebRequest to the second one?

EDIT: If I use a CookieCollection then this is coming back as empty. I just tried using WebClient as a last resort and even for that it is not working, the second request takes me back to the login screen. I noticed that in a WebBrowser there is a cookie.


回答1:


Add a CookieContainer to each request before you send it. Add the cookies you get from the first response to the second request. Assuming they use cookies for authentication, this should authenticate the second request.

 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;

 ...



回答2:


this is just a sample running code based on answer 2. Maybe be redundant maybe illustrate somebody.

            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:


This is a really old question and I know it states no WebClient but I will post here for everyone who comes across this from Google. The original concept is not my code. I do not know where I originally found it.

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 Class:

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;
  }
}


来源:https://stackoverflow.com/questions/5443667/httpwebrequest-pass-credentials-to-next-httpwebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!