C# ashx请求中的Session.SessionId持久性
当我对服务进行webrequest调用时,为什么它总是生成一个新的sessionid 这是我从sitea.com打电话的方式C# ashx请求中的Session.SessionId持久性,c#,asp.net,web-services,ashx,C#,Asp.net,Web Services,Ashx,当我对服务进行webrequest调用时,为什么它总是生成一个新的sessionid 这是我从sitea.com打电话的方式 WebClient fs = new WebClient(); var data = fs.DownloadData("http://siteb.com/serice.ashx"); var tostring = System.Text.Encoding.ASCII.GetString(data);
WebClient fs = new WebClient();
var data = fs.DownloadData("http://siteb.com/serice.ashx");
var tostring = System.Text.Encoding.ASCII.GetString(data);
return tostring;
这是siteb.com上的服务代码
[WebMethod(EnableSession = true)]
private string Read(HttpContext context)
{
var value = context.Session.SessionId;
if (value !=null) return value.ToString();
return "false";
}
每个请求的值总是不同的。我如何才能坚持这一点?来自MSDN: XML Web服务客户端由XML Web服务返回的HTTP cookie唯一标识。为了让XMLWeb服务维护客户端的会话状态,客户端必须持久化cookie。客户端可以通过创建CookieContainer的新实例并在调用XMLWeb服务方法之前将其分配给代理类的CookieContainer属性来接收HTTP cookie。如果您需要在代理类实例超出范围时维护会话状态,则客户端必须在对XML Web服务的调用之间持久化HTTP cookie。例如,Web表单客户端可以通过将CookieContainer保存在自己的会话状态来持久化HTTP cookie。由于并非所有XML Web服务都使用会话状态,因此并不总是要求客户端使用客户端代理的CookieContainer属性,因此XML Web服务的文档应说明是否使用会话状态 下面的代码示例是使用会话状态的XML Web服务的Web表单客户端。客户端通过将会话存储在客户端的会话状态来持久化唯一标识会话的HTTP cookie。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<html>
<script runat="server">
void EnterBtn_Click(Object Src, EventArgs E)
{
// Create a new instance of a proxy class for your XML Web service.
ServerUsage su = new ServerUsage();
CookieContainer cookieJar;
// Check to see if the cookies have already been saved for this session.
if (Session["CookieJar"] == null)
cookieJar= new CookieContainer();
else
cookieJar = (CookieContainer) Session["CookieJar"];
// Assign the CookieContainer to the proxy class.
su.CookieContainer = cookieJar;
// Invoke an XML Web service method that uses session state and thus cookies.
int count = su.PerSessionServiceUsage();
// Store the cookies received in the session state for future retrieval by this session.
Session["CookieJar"] = cookieJar;
// Populate the text box with the results from the call to the XML Web service method.
SessionCount.Text = count.ToString();
}
</script>
<body>
<form runat=server ID="Form1">
Click to bump up the Session Counter.
<p>
<asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
<p>
<asp:label id="SessionCount" runat=server/>
</form>
</body>
</html>
无效输入单击(对象Src、事件参数E)
{
//为XML Web服务创建代理类的新实例。
ServerUsage su=新的ServerUsage();
CookieContainer cookieJar;
//检查是否已保存此会话的Cookie。
if(会话[“CookieJar”]==null)
cookieJar=新CookieContainer();
其他的
cookieJar=(CookieContainer)会话[“cookieJar”];
//将CookieContainer分配给代理类。
su.CookieContainer=cookieJar;
//调用使用会话状态和cookie的XMLWeb服务方法。
int count=su.PerSessionServiceUsage();
//存储在会话状态下接收的cookie,以供此会话将来检索。
会话[“CookieJar”]=CookieJar;
//使用调用XMLWeb服务方法的结果填充文本框。
SessionCount.Text=count.ToString();
}
单击以增大会话计数器。
来自MSDN:
XML Web服务客户端由XML Web服务返回的HTTP cookie唯一标识。为了让XMLWeb服务维护客户端的会话状态,客户端必须持久化cookie。客户端可以通过创建CookieContainer的新实例并在调用XMLWeb服务方法之前将其分配给代理类的CookieContainer属性来接收HTTP cookie。如果您需要在代理类实例超出范围时维护会话状态,则客户端必须在对XML Web服务的调用之间持久化HTTP cookie。例如,Web表单客户端可以通过将CookieContainer保存在自己的会话状态来持久化HTTP cookie。由于并非所有XML Web服务都使用会话状态,因此并不总是要求客户端使用客户端代理的CookieContainer属性,因此XML Web服务的文档应说明是否使用会话状态
下面的代码示例是使用会话状态的XML Web服务的Web表单客户端。客户端通过将会话存储在客户端的会话状态来持久化唯一标识会话的HTTP cookie。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<html>
<script runat="server">
void EnterBtn_Click(Object Src, EventArgs E)
{
// Create a new instance of a proxy class for your XML Web service.
ServerUsage su = new ServerUsage();
CookieContainer cookieJar;
// Check to see if the cookies have already been saved for this session.
if (Session["CookieJar"] == null)
cookieJar= new CookieContainer();
else
cookieJar = (CookieContainer) Session["CookieJar"];
// Assign the CookieContainer to the proxy class.
su.CookieContainer = cookieJar;
// Invoke an XML Web service method that uses session state and thus cookies.
int count = su.PerSessionServiceUsage();
// Store the cookies received in the session state for future retrieval by this session.
Session["CookieJar"] = cookieJar;
// Populate the text box with the results from the call to the XML Web service method.
SessionCount.Text = count.ToString();
}
</script>
<body>
<form runat=server ID="Form1">
Click to bump up the Session Counter.
<p>
<asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
<p>
<asp:label id="SessionCount" runat=server/>
</form>
</body>
</html>
无效输入单击(对象Src、事件参数E)
{
//为XML Web服务创建代理类的新实例。
ServerUsage su=新的ServerUsage();
CookieContainer cookieJar;
//检查是否已保存此会话的Cookie。
if(会话[“CookieJar”]==null)
cookieJar=新CookieContainer();
其他的
cookieJar=(CookieContainer)会话[“cookieJar”];
//将CookieContainer分配给代理类。
su.CookieContainer=cookieJar;
//调用使用会话状态和cookie的XMLWeb服务方法。
int count=su.PerSessionServiceUsage();
//存储在会话状态下接收的cookie,以供此会话将来检索。
会话[“CookieJar”]=CookieJar;
//使用调用XMLWeb服务方法的结果填充文本框。
SessionCount.Text=count.ToString();
}
单击以增大会话计数器。
您必须接收会话id并将其传递给后续请求。默认情况下,它将在cookie中发送,但WebClient不处理cookie。您可以使用来解决此问题:
public class CookieAwareWebClient : WebClient
{
private CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = m_container;
}
return request;
}
}
只要重新使用相同的web客户端实例,就应该获得相同的会话id(如果会话当然不会超时)。您必须接收会话id并将其传递给后续请求。默认情况下,它将在cookie中发送,但WebClient不处理cookie。您可以使用来解决此问题:
public class CookieAwareWebClient : WebClient
{
private CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = m_container;
}
return request;
}
}
只要重用相同的web客户端实例,就应该获得相同的会话id(当然,如果会话不会超时)