簡體   English   中英

Web服務不會在請求之間保持狀態

[英]Web Service doesn't keep state between requests

我正在開發一個ASP.NET Web服務:

# MyWS.cs
[WebMethod(EnableSession = true)]
public bool TryLogin(string user, string pass)
{
    if (/* user validation is successful*/)
    {
        Context.Session["user"] = user;
        return true;
    }
    else
    {
        Context.Session.Abandon();
        return false;
    }
}
[WebMethod(EnableSession = true)]
public string RetrieveSomething()
{
    if (Context.Session["user"] == null)
        throw Exception("User hasn't logged in.");
    /* retrieve something */;
}

我也正在開發的PHP網站必須使用此ASP.NET:

# ws_client.php
function get_soap_client()
{
    if (!isset($_SESSION['client']))
        $_SESSION['client'] = new SoapClient('MyWS.asmx?WSDL');
    return $_SESSION['client'];
}
function try_login($user, $pass)
{
    return get_soap_client()->TryLogin(
        array('user' => $user, 'pass' => $pass))
        ->TryLoginResult;
}
function retrieve_something()
{
    return get_soap_client()->RetrieveSomething(
        array())->RetrieveSomethingResult;
}

# index.html
<?php
    if (isset($_POST['submit']))
    {
        session_start();
        require_once('ws_client.php');
        if (try_login($_POST['user'],
                      $_POST['pass']))
        {
            session_write_close();
            header('Location: /main.php');
            exit();
        }
?>
<html> <!-- login form here >

# main.php
<?php
    session_start();
    require_once('ws_client.php');
    // Here I get the Exception "User hasn't logged in.", even though
    // the user has logged in and the web service has already been notified.
    echo htmlspecialchars(retrieve_something());
?>

我的Web服務或PHP站點怎么了?

我不知道PHP SOAP工具,但是Session狀態通過cookie維護。 此代碼是否會在第一次接受Cookie,然后在隨后的調用中將其發送回去?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM