如何通过HttpWebRequest正确地携带SessionID? [英] How to correctly carry over SessionID via HttpWebRequest?

查看:114
本文介绍了如何通过HttpWebRequest正确地携带SessionID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Session.SessionID来支持旧的sessionID。使用HttpWebRequest CookieContainer来获取所需的

页面会话变量,但它没有按预期工作。

HttpResponse对象总是从旧的

中返回一个不同的sessionID,我试图强制它。为什么objRequest没有携带

旧的SessionID?


private String ReadHtmlPage(string url)

{

String result = string.Empty;

CookieContainer myContainer = new CookieContainer();

HttpWebRequest objRequest =

(HttpWebRequest)WebRequest .Create(url);

objRequest.Method =" GET";

objRequest.UserAgent =" Mozilla / 4.0(兼容; MSIE

6.0; Windows NT5.0; .NET CLR 1.0.2914)" ;;

objRequest.CookieContainer = new CookieContainer();

Cookie c = new Cookie( );

c.Name =" ASP.NET_SessionId";

c.Value = Session.SessionID;

c.Domain =" ; http:// localhost / mysite /" ;;

myContainer.Add(c);

Response.Write(" OLD SessionID - " + Session.SessionID +

"< br>");

HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse();

//保留饼干

foreach(饼干厨师在objResponse.Cookies)

{

Response.Write(" New SessionID - + cook.Name +

cook.Value + cook.Domain +"< br>");

}


//查看html。

使用(StreamReader sr =

new StreamReader(objResponse.GetResponseStream())



{

result = sr.ReadToEnd();


//关闭并清理StreamReader

sr.Close();

}


返回结果;

}


谢谢

Rod

I am trying to foward the old sessionID using "Session.SessionID" to
an HttpWebRequest CookieContainer so that I can capture the requested
page session variables but it is not working as it is supposed to. The
HttpResponse object always returns a different sessionID from the old
one which I am trying to force. Why is objRequest not carrying over the
old SessionID?

private String ReadHtmlPage(string url)
{
String result = string.Empty;
CookieContainer myContainer = new CookieContainer();
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "GET";
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT5.0; .NET CLR 1.0.2914)";
objRequest.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
c.Name = "ASP.NET_SessionId";
c.Value = Session.SessionID;
c.Domain = "http://localhost/mysite/";
myContainer.Add(c);
Response.Write("OLD SessionID -" + Session.SessionID +
"<br>");
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
//retain the cookies
foreach (Cookie cook in objResponse.Cookies)
{
Response.Write("New SessionID -" + cook.Name +
cook.Value + cook.Domain + "<br>");
}

//Check out the html.
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream())
)
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Thanks
Rod

推荐答案

Rod,


为什么不直接存储你附加到第一个HttpWebRequest的CookieContainer实例,然后将它附加到第二个HttpWebRequest?

这样,cookie就会从请求传递请求。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP ]

- mv*@spam.guard.caspershouse.com

& LT; rl ******** @ gmail.com写信息

新闻:11 ********************** @ m73g2000cwd.googlegr oups.com ...
Rod,

Why not just store the CookieContainer instance which you attached to
the first HttpWebRequest, and then attach that to the second HttpWebRequest?
This way, the cookies are passed from request to request.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

>我正在尝试使用Session.SessionID进行旧的sessionID。使用HttpWebRequest CookieContainer来获取所需的

页面会话变量,但它没有按预期工作。

HttpResponse对象总是从旧的

中返回一个不同的sessionID,我试图强制它。为什么objRequest没有携带

旧的SessionID?


private String ReadHtmlPage(string url)

{

String result = string.Empty;

CookieContainer myContainer = new CookieContainer();

HttpWebRequest objRequest =

(HttpWebRequest)WebRequest .Create(url);

objRequest.Method =" GET";

objRequest.UserAgent =" Mozilla / 4.0(兼容; MSIE

6.0; Windows NT5.0; .NET CLR 1.0.2914)" ;;

objRequest.CookieContainer = new CookieContainer();

Cookie c = new Cookie( );

c.Name =" ASP.NET_SessionId";

c.Value = Session.SessionID;

c.Domain =" ; http:// localhost / mysite /" ;;

myContainer.Add(c);

Response.Write(" ;旧会话ID - " + Session.SessionID +

"< br>");

HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse() ;

//保留饼干

foreach(饼干厨师在objResponse.Cookies)

{

回应。写(New SessionID - " + cook.Name +

cook.Value + cook.Domain +"< br>");

} < br $>

//查看html。

使用(StreamReader sr =

new StreamReader(objResponse.GetResponseStream())



{

result = sr.ReadToEnd();


//关闭并清理StreamReader

sr.Close();

}


返回结果;

}


谢谢

Rod
>I am trying to foward the old sessionID using "Session.SessionID" to
an HttpWebRequest CookieContainer so that I can capture the requested
page session variables but it is not working as it is supposed to. The
HttpResponse object always returns a different sessionID from the old
one which I am trying to force. Why is objRequest not carrying over the
old SessionID?

private String ReadHtmlPage(string url)
{
String result = string.Empty;
CookieContainer myContainer = new CookieContainer();
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "GET";
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT5.0; .NET CLR 1.0.2914)";
objRequest.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
c.Name = "ASP.NET_SessionId";
c.Value = Session.SessionID;
c.Domain = "http://localhost/mysite/";
myContainer.Add(c);
Response.Write("OLD SessionID -" + Session.SessionID +
"<br>");
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
//retain the cookies
foreach (Cookie cook in objResponse.Cookies)
{
Response.Write("New SessionID -" + cook.Name +
cook.Value + cook.Domain + "<br>");
}

//Check out the html.
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream())
)
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Thanks
Rod



Nicholas,


就是这样。没有两个请求,只有一个。如果只有一页要求

,为什么我要b $ b需要两个网页请求?我假设Session.SessionID将获取当前会话

的值,对吗?我很困惑......抱歉..


Rod


Paldino [.NET / C#MVP]写道:
Nicholas,

That is the thing. There are no two requests, just one. Why would I
need to have two web requests if there is just one page to be
requested? I am assuming that Session.SessionID will grab the value for
the current session, correct? I am confused... sorry..

Rod

Paldino [.NET/C# MVP] wrote:

Rod,


为什么不直接存储你附加的CookieContainer实例

第一个HttpWebRequest,然后附上到第二个HttpWebRequest?

这样,cookie就会从请求传递到请求。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< rl ******** @ gmail.comwrote in message

news:11 ********************** @ m73g2000cwd.googlegr oups.com ...
Rod,

Why not just store the CookieContainer instance which you attached to
the first HttpWebRequest, and then attach that to the second HttpWebRequest?
This way, the cookies are passed from request to request.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...

我正在尝试使用Session.SessionID来转发旧的sessionID。使用HttpWebRequest CookieContainer来获取所需的

页面会话变量,但它没有按预期工作。

HttpResponse对象总是从旧的

中返回一个不同的sessionID,我试图强制它。为什么objRequest没有携带

旧的SessionID?


private String ReadHtmlPage(string url)

{

String result = string.Empty;

CookieContainer myContainer = new CookieContainer();

HttpWebRequest objRequest =

(HttpWebRequest)WebRequest .Create(url);

objRequest.Method =" GET";

objRequest.UserAgent =" Mozilla / 4.0(兼容; MSIE

6.0; Windows NT5.0; .NET CLR 1.0.2914)" ;;

objRequest.CookieContainer = new CookieContainer();

Cookie c = new Cookie( );

c.Name =" ASP.NET_SessionId";

c.Value = Session.SessionID;

c.Domain =" ; http:// localhost / mysite /" ;;

myContainer.Add(c);

Re sponse.Write(" OLD SessionID - " + Session.SessionID +

"< br>");

HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse() ;

//保留饼干

foreach(饼干厨师在objResponse.Cookies)

{

回应。写(New SessionID - " + cook.Name +

cook.Value + cook.Domain +"< br>");

} < br $>

//查看html。

使用(StreamReader sr =

new StreamReader(objResponse.GetResponseStream())



{

result = sr.ReadToEnd();


//关闭并清理StreamReader

sr.Close();

}


返回结果;

}


谢谢

Rod
I am trying to foward the old sessionID using "Session.SessionID" to
an HttpWebRequest CookieContainer so that I can capture the requested
page session variables but it is not working as it is supposed to. The
HttpResponse object always returns a different sessionID from the old
one which I am trying to force. Why is objRequest not carrying over the
old SessionID?

private String ReadHtmlPage(string url)
{
String result = string.Empty;
CookieContainer myContainer = new CookieContainer();
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "GET";
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT5.0; .NET CLR 1.0.2914)";
objRequest.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
c.Name = "ASP.NET_SessionId";
c.Value = Session.SessionID;
c.Domain = "http://localhost/mysite/";
myContainer.Add(c);
Response.Write("OLD SessionID -" + Session.SessionID +
"<br>");
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
//retain the cookies
foreach (Cookie cook in objResponse.Cookies)
{
Response.Write("New SessionID -" + cook.Name +
cook.Value + cook.Domain + "<br>");
}

//Check out the html.
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream())
)
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Thanks
Rod


Rod,

你是对的,一页没有两个请求,但是如果你只是要从网站上下载一页,那么为什么你需要< br $>
会话ID?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< rl ***** ***@gmail.com在留言中写道

新闻:11 ********************** @ p79g2000cwp.googlegr psps.com。 ..
Rod,

You are right, there isn''t two requests for one page, but if you are
only ever going to download one page from the site, then why do you need the
session id?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...

Nicholas,


就是这样。没有两个请求,只有一个。如果只有一页要求

,为什么我要b $ b需要两个网页请求?我假设Session.SessionID将获取当前会话

的值,对吗?我很困惑......抱歉..


Rod


Paldino [.NET / C#MVP]写道:
Nicholas,

That is the thing. There are no two requests, just one. Why would I
need to have two web requests if there is just one page to be
requested? I am assuming that Session.SessionID will grab the value for
the current session, correct? I am confused... sorry..

Rod

Paldino [.NET/C# MVP] wrote:

> Rod,

为什么不直接存储你附加到第一个HttpWebRequest的CookieContainer实例,然后将其附加到第二个
HttpWebRequest?
这样,cookie就会从请求传递到请求。

希望这会有所帮助。

-
- Nicholas Paldino [ .NET / C#MVP]
- mv*@spam.guard.caspershouse.com

< rl********@gmail.com在留言中写道
新闻:11 ********************** @ m73g2000cwd .googleg roups.com ...
>Rod,

Why not just store the CookieContainer instance which you attached to
the first HttpWebRequest, and then attach that to the second
HttpWebRequest?
This way, the cookies are passed from request to request.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...

>我正在尝试使用Session.SessionID进行旧的sessionID。使用HttpWebRequest CookieContainer来获取所需的

页面会话变量,但它没有按预期工作。

HttpResponse对象总是从旧的

中返回一个不同的sessionID,我试图强制它。为什么objRequest没有携带

旧的SessionID?


private String ReadHtmlPage(string url)

{

String result = string.Empty;

CookieContainer myContainer = new CookieContainer();

HttpWebRequest objRequest =

(HttpWebRequest)WebRequest .Create(url);

objRequest.Method =" GET";

objRequest.UserAgent =" Mozilla / 4.0(兼容; MSIE

6.0; Windows NT5.0; .NET CLR 1.0.2914)" ;;

objRequest.CookieContainer = new CookieContainer();

Cookie c = new Cookie( );

c.Name =" ASP.NET_SessionId";

c.Value = Session.SessionID;

c.Domain =" ; http:// localhost / mysite /" ;;

myContainer.Add(c);

Response.Write(" ;旧会话ID - " + Session.SessionID +

"< br>");

HttpWebResponse objResponse =

(HttpWebResponse)objRequest.GetResponse() ;

//保留饼干

foreach(饼干厨师在objResponse.Cookies)

{

回应。写(New SessionID - " + cook.Name +

cook.Value + cook.Domain +"< br>");

} < br $>

//查看html。

使用(StreamReader sr =

new StreamReader(objResponse.GetResponseStream())



{

result = sr.ReadToEnd();


//关闭并清理StreamReader

sr.Close();

}


返回结果;

}


谢谢

Rod
>I am trying to foward the old sessionID using "Session.SessionID" to
an HttpWebRequest CookieContainer so that I can capture the requested
page session variables but it is not working as it is supposed to. The
HttpResponse object always returns a different sessionID from the old
one which I am trying to force. Why is objRequest not carrying over the
old SessionID?

private String ReadHtmlPage(string url)
{
String result = string.Empty;
CookieContainer myContainer = new CookieContainer();
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "GET";
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT5.0; .NET CLR 1.0.2914)";
objRequest.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
c.Name = "ASP.NET_SessionId";
c.Value = Session.SessionID;
c.Domain = "http://localhost/mysite/";
myContainer.Add(c);
Response.Write("OLD SessionID -" + Session.SessionID +
"<br>");
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
//retain the cookies
foreach (Cookie cook in objResponse.Cookies)
{
Response.Write("New SessionID -" + cook.Name +
cook.Value + cook.Domain + "<br>");
}

//Check out the html.
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream())
)
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Thanks
Rod



这篇关于如何通过HttpWebRequest正确地携带SessionID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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