HttpWebRequest与多个Set-Cookie [英] HttpWebRequest with multiple Set-Cookie

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

问题描述

我尝试使用httpwebrequest登录到wordpress,但无法这样做,响应标头中有多个set-cookie,很少的cookie丢失,因此无法显示仪表板,但是我能够使用套接字,但因为我所有的编码是使用httwebrequest构建的,我不能切换到套接字。



响应头

 (Status-Line)HTTP / 1.1 302暂时移动
日期Wed,23 Mar 2011 07:52:24 GMT
服务器Apache
X-Powered-由PHP /5.2.17
Expires:Wed,11 Jan 1984 05:00:00 GMT
Cache-Control no-cache,必须重新验证,max-age = 0
Pragma no-cache
Set-Cookie wordpress_test_cookie = WP + Cookie + check; path = /
Set-Cookie wordpress_6e7750f6a474de23330d1b7f0e9990c6 = admin%7C1301039544%7C16d39b9f49cf062500d50471df7320e9; path = / wp-content / plugins; httponly
Set-Cookie wordpress_6e7750f6a474de23330d1b7f0e9990c6 = admin%7C1301039544%7C16d39b9f49cf062500d50471df7320e9; path = / wp-admin; httponly
Set-Cookie wordpress_logged_in_6e7750f6a474de23330d1b7f0e9990c6 = admin%7C1301039544%7C0b2d990d7cc420c4203fbce464c285b2; path = /; httponly
Last-Modified Wed,23 Mar 2011 07:52:24 GMT
位置http://breakingnewstoday.net/wp-admin/
Keep-Alive timeout = 3,max = 100
连接Keep-Alive
传输编码块
Content-Type text / html; charset = UTF-8

请任何人建议如何使用httpwebrequest作业,



在使用HttpWebRequest post(http://mywebsite.net/wp-login.php)时,httpwebrequest会处理来自流的Cookie, ,
log = user1& pwd = pass1& wp-submit = Log + In& redirect_to = http://mywebsite.net/wp-admin/& testcookie = 1



它返回登录页面,是的我已经检查了一切10次,如果使用套接字,手动cookie处理相同的工作。



Cookie在手动套接字Set-Cookie中返回以下内容:



wordpress_6e7750f6a474de23330d1b7f0e9990c6 = admin%7C1301040547%7C2463ecd9d363899be1129ae53287963c; path = / wp-content / plugins; httponly >

同样在httpwebresponse是:



wordpress_sec_6e7750f6a474de23330d1b7f0e9990c6 = +; expires = Tue,2010年3月23日08:12: 00 GMT;



此处缺少的值= +

解决方案

您忘记设置HttpWebRequest.CookieContainer吗?



默认情况下,它为null,这意味着请求标头中不会发送Cookie,也不会返回任何Cookie保留后用。


I am trying to login to wordpress using httpwebrequest, but unable to do so, there are multiple set-cookie in the response header, few cookies are lost and so unable to show the dashboard, however i am able to login using sockets, but as my all coding is built using httwebrequest i cannot switch to sockets.

response headers

(Status-Line)   HTTP/1.1 302 Moved Temporarily
Date             Wed, 23 Mar 2011 07:52:24 GMT
Server         Apache
X-Powered-By    PHP/5.2.17
Expires:        Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control   no-cache, must-revalidate, max-age=0
Pragma         no-cache
Set-Cookie     wordpress_test_cookie=WP+Cookie+check; path=/
Set-Cookie     wordpress_6e7750f6a474de23330d1b7f0e9990c6=admin%7C1301039544%7C16d39b9f49cf062500d50471df7320e9; path=/wp-content/plugins; httponly
Set-Cookie     wordpress_6e7750f6a474de23330d1b7f0e9990c6=admin%7C1301039544%7C16d39b9f49cf062500d50471df7320e9; path=/wp-admin; httponly
Set-Cookie     wordpress_logged_in_6e7750f6a474de23330d1b7f0e9990c6=admin%7C1301039544%7C0b2d990d7cc420c4203fbce464c285b2; path=/; httponly
Last-Modified   Wed, 23 Mar 2011 07:52:24 GMT
Location         http://breakingnewstoday.net/wp-admin/
Keep-Alive     timeout=3, max=100
Connection     Keep-Alive
Transfer-Encoding   chunked
Content-Type    text/html; charset=UTF-8

Please can any one suggest how to use httpwebrequest to the job, do i overwrite httpwebrequest to handle cookies from stream, if yes how do i do it?

On using HttpWebRequest post ("http://mywebsite.net/wp-login.php", "log=user1&pwd=pass1&wp-submit=Log+In&redirect_to=http://mywebsite.net/wp-admin/&testcookie=1"

it returns login page, yes i have checked everything 10 times, and the same works if used with sockets, with manual cookie handling.

the cookie in manual socket 'Set-Cookie' returns following:

"wordpress_6e7750f6a474de23330d1b7f0e9990c6=admin%7C1301040547%7C2463ecd9d363899be1129ae53287963c; path=/wp-content/plugins; httponly"

same in httpwebresponse is:

"wordpress_sec_6e7750f6a474de23330d1b7f0e9990c6=+; expires=Tue, 23-Mar-2010 08:12:00 GMT;"

the value is missing here =+

解决方案

Did you forget to set HttpWebRequest.CookieContainer?

By default, it would be null, meaning no cookies would be sent in request header, and no cookies from response would be preserved for latter usage.

这篇关于HttpWebRequest与多个Set-Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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