【问题标题】:Usage of web services with session-state cookieless=true使用会话状态 cookieless=true 的 Web 服务
【发布时间】:2014-05-23 22:41:49
【问题描述】:

在这样的网络服务方法中启用会话:

[WebMethod(EnableSession=true)]
public string HelloWorld()
{
    return "Hello World";
}

使用无 cookie 会话状态 (web.config):

<sessionState cookieless="true"></sessionState>

然后尝试像这样从客户端调用它:

localhost.WebService1 ws1 = new localhost.WebService1();    // the web service proxy        
ws1.HelloWorld();

您会收到一个重定向 WebException (302),说明对象已被移动:

【问题讨论】:

  • localhost.WebService1 的基类是什么?
  • SoapHttpClientProtocol(命名空间“System.Web.Services.Protocols”),因为它是由 .NET 自动生成的。
  • 那里有布尔属性“AllowAutoRedirect”,请检查是否默认为false,改为true,测试一下
  • 请看我对你的回答的评论。

标签: asp.net web-services session-state webmethod cookieless


【解决方案1】:

查看SoapHttpClientProtocol的文档,属性“AllowAutoRedirect”的默认值为false。

http://msdn.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.allowautoredirect%28v=vs.110%29.aspx

在调用 web 方法之前将其更改为 true 将自动处理 302 http 重定向响应。

【讨论】:

  • 这不起作用,并给出一个错误(正如我在回答中链接到的 Microsoft 文章中所述):客户端发现响应内容类型为 'text/html; charset=utf-8',但应为 'text/xml'
【解决方案2】:

一篇微软文章描述了这个问题:http://msdn.microsoft.com/en-us/library/aa480509.aspx

来自客户端的调用必须捕获 WebException,并更新 Web 服务的 URL,该 URL 必须包含 Web 服务器生成的 sessionId。然后重复调用该方法:

localhost.WebService1 ws1 = new localhost.WebService1();    // the web service proxy    
try {
    ws1.HelloWorld();
} catch (WebException ex) {
    HttpWebResponse response = (HttpWebResponse)ex.Response;
    if (response.StatusCode == HttpStatusCode.Found) {
        ws1.Url = new Uri(new Uri(ws1.Url), response.Headers["Location"]).AbsoluteUri;
        ws1.HelloWorld();
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2012-10-14
    • 2016-07-05
    • 2020-08-20
    • 2011-02-10
    相关资源
    最近更新 更多