HttpWebRequest& HttpWebResponse Cookies [英] HttpWebRequest & HttpWebResponse Cookies

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

问题描述

我有一个Windows窗体应用程序,它生成请求,下载图像,并等待用户

输入登录信息。不幸的是,这个图像是动态的,并且基于会话数据。我在HttpWebRequest的CookieCollection属性上读取了
的文档。目前,我的代码中有

功能,可以接受cookie,并在新的HttpWebRequest时返回它们;

但是,在进一步检查返回的HttpWebResponse时。 Cookies属性,我注意到

不会返回关于我的会话数据应该存在的cookie(一个这样的名为,
cookie命名为JSESSIONID)。还有哪些其他方法可以检索

包含会话数据的cookie?


这是一段代码片段(请原谅wordwrap)


Dim strURL As String =" https://www.thisurlisatest.com/"

Dim objHTTPRequest As HttpWebRequest = CType(WebRequest.Create(strURL) ,HttpWebRequest)

objHTTPRequest.CookieContainer = New CookieContainer()

Dim objHTTPResponse As HttpWebResponse = CType(objHTTPRequest.GetResponse(),HttpWebResponse)

Dim objReceiveStream As Stream = objHTTPResponse.GetResponseStream()

''我在这里成功检索到了cookies

m_Cookies = objHTTPResponse.Cookies

Dim objCookie As Cookie

每个objCookie在m_Cookies中

Console.WriteLine(objCookie.Name +" =" + objCookie.Value)

下一个


''这只输出:language_pref = en_US

''应该有更多包含会话ID数据的cookie


提前感谢你可以解决这个问题,


Jeff Cheung

I have a windows form application that generates a request, downloads an image, and waits the user
to enter in login info. Unfortunately, this image is dynamic and based on session data. I have
read documents on the CookieCollection property of HttpWebRequest. Currently, I have the
functionality in my code to be able to accept cookies, and return them upon a new HttpWebRequest;
however, upon further inspection of the returning HttpWebResponse.Cookies property, I notice that
the cookies that are supposed to be there regarding my session data are not returned (one such
cookie named, JSESSIONID). What other methods are there available to me to retrieve cookies that
would contain session data?

Here is a code snippet (please excuse the wordwrap)

Dim strURL As String = "https://www.thisurlisatest.com/"
Dim objHTTPRequest As HttpWebRequest = CType(WebRequest.Create(strURL), HttpWebRequest)
objHTTPRequest.CookieContainer = New CookieContainer()
Dim objHTTPResponse As HttpWebResponse = CType(objHTTPRequest.GetResponse(), HttpWebResponse)
Dim objReceiveStream As Stream = objHTTPResponse.GetResponseStream()
'' I successfully retrieve the cookies here
m_Cookies = objHTTPResponse.Cookies
Dim objCookie As Cookie
For Each objCookie In m_Cookies
Console.WriteLine(objCookie.Name + "=" + objCookie.Value)
Next

'' This only outputs this: language_pref=en_US
'' There should be more cookies that contain session id data

Thanks in advance for any light you can shed on this matter,

Jeff Cheung

推荐答案

Hi Cheung,


您是否有多个使用cookieCollection的网络服务器?


否则你可以看看正常的HttpRequest.cookie类


它更容易处理(如果你不忘记设置实时时间)


我希望这有帮助


Cor
Hi Cheung,

Do you have multiple webservers that you use the cookieCollection?

Otherwise you can have a look at the normal HttpRequest.cookie class

It is much easier to handle (if you dont forget to set the live time)

I hope this helps

Cor


Cor,


我没有将cookie集合传递给多个web服务器;但是,这是一个Windows窗体

应用程序。你如何建议我在表单应用程序中获取System.Web.HttpRequest对象?


我确实找到了解决问题的方法;然而,不幸的是,它给我带来了一个更大的问题。

我最终看到了HttpWebResponse对象中Headers属性中的Set-Cookie项目,

并添加了值为HttpWebRequest对象的Headers属性中的Cookie项。只要我没有在HttpWebRequest中传递CookieCollection,这个

就可以完美运行。


根据这个链接:

< a rel =nofollowhref =http://authors.aspalliance.com/damianm/article/4/5/httpWebRequest.aspx.vb.view.aspx\"target =_ blank> http://authors.aspalliance .com / damia ... x.vb.view.aspx


如果我填充CookieCollection,这将覆盖请求对象中的Cookie标头。


所以我现在的问题是我能够成功登录并保留我的会话,但任何后续的

页面请求都不会在HttpWebResponse.GetResponseStream的ResponseStream中返回任何内容()。

查看HttpWebResponse.StatusCode得到200,但每当我发出任何请求时,内容长度始终为0。任何想法?


提前致谢,

Jeff Cheung


Cor写道:
Cor,

I''m not passing the cookie collection to multiple webservers; however, this is a windows form
application. How do you propose that I get the System.Web.HttpRequest object in my form application?

I did find the solution to my problem; however, it brought upon a bigger issue for me unfortunately.
I ended up looking at the Set-Cookie item in the Headers property in the HttpWebResponse object,
and adding that value to the Cookie item in the Headers property in the HttpWebRequest object. This
works flawlessly as long as I do not pass a CookieCollection in the HttpWebRequest.

According to this link:

http://authors.aspalliance.com/damia...x.vb.view.aspx

if I populate the CookieCollection, this will override the Cookie header in the request object.

So my problem now is that I''m able to successfully login and retain my session, but any subsequent
page request returns nothing in the ResponseStream from HttpWebResponse.GetResponseStream().
Looking at the HttpWebResponse.StatusCode yields 200, but the content-length is always 0 whenever I
make any requests. Any ideas?

Thanks in advance,

Jeff Cheung

Cor wrote:
你好,

你有多个使用cookieCollection的网络服务器吗?

否则你可以看看正常的HttpRequest .cookie class

它更容易处理(如果你不忘记设置实时时间)

我希望这有帮助

Cor
Hi Cheung,

Do you have multiple webservers that you use the cookieCollection?

Otherwise you can have a look at the normal HttpRequest.cookie class

It is much easier to handle (if you dont forget to set the live time)

I hope this helps

Cor



Hi Cheung,


我从未使用过那个cookiecontainer因为其他的在我看来

更简单。


但我的cookies问题始终是我忘记设置

cookie的实时时间,它我没有在你的代码中看到,也许这也是你的问题,所以在我开始检查以前我从未做过的事情之前,我不会在你的代码中看到这个问题。:
你能自己查一下吗?


Cor
Hi Cheung,

I never used that cookiecontainer because the other ones are in my opinion
more simple.

But my problem with cookies is always that I forget to set the livetime of a
cookie, it disapears immidiatly.

I did not see that in your code, maybe it is your problem also, so before I
start checking things that I never did before, can you check that yourself?

Cor


这篇关于HttpWebRequest&amp; HttpWebResponse Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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