2011-01-11 106 views
1

之間的狀態,我開發一個ASP.NET Web服務: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 */; 
} 

這ASP.NET必須由PHP的網站,我發展以及消費:

# 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站點都可能出錯?

回答

1

我不知道PHP SOAP工具,但會話狀態是通過cookie維護的。此代碼是否會首次接受cookie,然後在隨後的調用中將其發回?

+0

我不知道。我應該如何在第二個請求(`RequestSomething` WebMethod)上讀取cookie? – pyon 2011-01-11 18:10:46