HttpWebRequest的凭据传递到下一个HttpWebRequest的 [英] HttpWebRequest pass credentials to next HttpWebRequest

查看:196
本文介绍了HttpWebRequest的凭据传递到下一个HttpWebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我登录到网页使用HttpWebRequest和获取的一些信息。然后我用这些信息来创建一个新的HttpWebRequest得到一些更多的信息。我不希望使用Web客户端。

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.

我如何能够通过我不能登录使用HttpWebRequest的第一到第二个获得该证书?

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

编辑:如果我用一个CookieCollection那么这是回来为空。我只是尝试使用Web客户端作为最后的手段,甚至对于它不工作,第二个请求把我带回到登录屏幕。我注意到,在web浏览器有一个cookie。

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.

推荐答案

添加<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx">CookieContainer每个请求,然后再发送。加入你的第二个请求的第一反应得到的饼干。假设它们使用Cookie进行身份验证,这应该验证第二个请求。

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;

 ...

这篇关于HttpWebRequest的凭据传递到下一个HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆