2012-07-13 35 views
1

当我做一个webrequest调用服务时,为什么它总是生成一个新的sessionid?Session.SessionId持久性请求到ashx

这是我从sitea.com

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"; 
    } 

值始终为每个请求的不同。我该如何坚持下去?

回答

3

你必须接受会话ID,并把它传递给后续请求阅读完整细节。默认情况下,它将通过cookie发送,但WebClient不处理cookie。您可以使用CookieAwareWebClient来解决这个问题:

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(如果会话不会超时当然)。

+0

适合我。很好的解决方案! – 2015-10-05 14:00:52

2

来自MSDN: XML Web服务客户端由XML Web服务返回的HTTP cookie唯一标识。为了让XML Web服务维护客户端的会话状态,客户端必须持久保存cookie。在调用XML Web服务方法之前,客户端可以通过创建CookieContainer的新实例并将其分配给代理类的CookieContainer属性来接收HTTP cookie。如果您需要在代理类实例超出范围时维护会话状态,那么客户端必须在对XML Web服务的调用之间保持HTTP cookie。例如,Web窗体客户端可以通过将CookieContainer保存在自己的会话状态中来保存HTTP cookie。由于并非所有XML Web服务都使用会话状态,因此客户端并不总是需要使用客户端代理的CookieContainer属性,因此XML Web服务的文档应说明是否使用会话状态。

以下代码示例是使用会话状态的XML Web服务的Web窗体客户端。客户端通过将HTTP cookie存储在客户端的会话状态中来保存唯一标识会话的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> 

MSDN上http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx