dpmpa26468 2011-01-11 18:02
浏览 73
已采纳

Web Service不会在请求之间保持状态

I am developing an ASP.NET Web Service:

# 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 */;
}

This ASP.NET must be consumed by a PHP Web site I am developing as well:

# 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());
?>

What could be wrong with either my Web Service or my PHP site?

  • 写回答

1条回答 默认 最新

  • douyong1850 2011-01-11 18:06
    关注

    I don't know the PHP SOAP tools, but Session state is maintained through a cookie. Will this code accept a cookie the first time, then send it back on subsequent calls?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效