HttpWebRequest with multiple Set-Cookie

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 16:32:12

问题


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 =+


回答1:


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.




回答2:


I can only suspect, so I may be wrong. Each HttpWebRequest is a new connection, and so a new session is created. The cookies become invalid because of different session. I think Encoding chunked and keep-alive shows that the current connection is somewhat important.

I believe you're using the same socket in the same connection to send all your cookies.

Hope this explains your problem. If not, a bit of code would be useful for further troubleshooting.



来源:https://stackoverflow.com/questions/5402267/httpwebrequest-with-multiple-set-cookie

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