1

サービスに対してwebrequest呼び出しを行うと、なぜ常に新しいセッションIDが生成されるのですか?

これは私が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";
    }

値はリクエストごとに常に異なります。どうすればこれを持続できますか?

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サービスクライアントは、XMLWebサービスによって返されるHTTPCookieによって一意に識別されます。XML Webサービスがクライアントのセッション状態を維持するには、クライアントがCookieを永続化する必要があります。クライアントは、CookieContainerの新しいインスタンスを作成し、XML Webサービスメソッドを呼び出す前にそれをプロキシクラスのCookieContainerプロパティに割り当てることにより、HTTPCookieを受信できます。プロキシクラスインスタンスがスコープ外になるときを超えてセッション状態を維持する必要がある場合、クライアントはXMLWebサービスへの呼び出し間でHTTPCookieを永続化する必要があります。たとえば、Webフォームクライアントは、CookieContainerを独自のセッション状態で保存することにより、HTTPCookieを永続化できます。すべてのXMLWebサービスがセッション状態を使用するわけではないため、クライアントはクライアントプロキシのCookieContainerプロパティを使用する必要がない場合があります。

次のコード例は、セッション状態を使用するXMLWebサービスのWebフォームクライアントです。クライアントは、セッションをクライアントのセッション状態に保存することにより、セッションを一意に識別するHTTPCookieを保持します。

<%@ 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>

MSDNhttp ://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspxで完全な詳細をお読みください

于 2012-07-13T19:36:06.990 に答える