1

when i make a webrequest call to a service, why does it always generates a new sessionid?

This is how i call from 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;

This is the service code at siteb.com

[WebMethod(EnableSession = true)]
    private string Read(HttpContext context)
    {
        var value = context.Session.SessionId;
        if (value !=null) return value.ToString();
            return "false";
    }

value is always different for every request. How can i persist this?

4

2 回答 2

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

于 2012-07-13T19:47:06.117 回答
2

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

下面的代码示例是使用会话状态的 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>

阅读 MSDN http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx上的完整详细信息

于 2012-07-13T19:36:06.990 回答