Java 如何将会话 ID 作为 Soap 请求的一部分传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23113574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:47:23  来源:igfitidea点击:

How to pass session id as part of Soap request?

javaspringsessionsoapsoapui

提问by blue-sky

I invoke an authentication request in order receive a session id :

我调用身份验证请求以接收会话 ID:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <loginResponse xmlns="urn:company" xmlns:ns2="urn:company">
         <result>
            <sessionId>2342422342.dc8bizxsfapi03</sessionId>
            <msUntilPwdExpiration>2342342342342353452323</msUntilPwdExpiration>
         </result>
      </loginResponse>
   </S:Body>
</S:Envelope>

In the docs for the Soap API that I'm using it states :

在我使用的 Soap API 的文档中指出:

A successful login will return a
session ID as an HTTP Cookie. This cookie must be passed back to all subsequent HTTP
Requests that invoke API operations in order to authenticate.

How is the session id passed to the next http reqeust as this is not described ?

会话 id 如何传递到下一个 http 请求,因为这没有描述?

I'm assuming I need to embed the session ID within an XML tag as part of the subsequesnt request but this should be detailed in the API or is there a standard mechanism I can use ?

我假设我需要将会话 ID 嵌入 XML 标记中作为后续请求的一部分,但这应该在 API 中详细说明,或者是否有我可以使用的标准机制?

采纳答案by Peter G

The API documentation you reference states that the service will set a cookie on the response that needs to be set on any subsequent request. Cookies are sent via HTTP headers, not the body of of the request/response, and are commonly used to establish and maintain sessions. The underlying HTTP client library the web service framework uses is well equipped to handle this for you, but because SOAP web services are designed to be stateless, you usually have to ask the framework if you want it to maintain sessions. When you enable this functionality, it simply means the framework will send any cookies back to the server that the server sends to it, which is precisely what your SOAP API documentation is asking that you do.

您参考的 API 文档指出,该服务将在需要在任何后续请求上设置的响应上设置 cookie。Cookie 是通过 HTTP 标头而不是请求/响应的正文发送的,通常用于建立和维护会话。Web 服务框架使用的底层 HTTP 客户端库可以很好地为您处理这个问题,但由于 SOAP Web 服务被设计为无状态的,您通常必须询问框架是否希望它维护会话。当您启用此功能时,它只是意味着框架会将任何 cookie 发送回服务器发送给它的服务器,这正是您的 SOAP API 文档要求您执行的操作。

To enable this functionality in jax-ws, you set BindingProvider.SESSION_MAINTAIN_PROPERTY to true on the RequestContext. This articlegives an example and more details.

要在 jax-ws 中启用此功能,请在 RequestContext 上将 BindingProvider.SESSION_MAINTAIN_PROPERTY 设置为 true。 这篇文章给出了一个例子和更多的细节。

回答by swist

Yes, that is a popular mechanism.

是的,这是一种流行的机制。

As the API states the session key was returned in the Cookie (it is a HTTP header). What is in a response body is only a repetition of the session key (I hope so). You need to extract the cookie from HTTP headers. If you are using the JAX-WS you may enable the session awareness using BindingProvider.SESSION_MAINTAIN_PROPERTY:

正如 API 声明的那样,会话密钥是在 Cookie 中返回的(它是一个 HTTP 标头)。响应正文中的内容只是会话密钥的重复(我希望如此)。您需要从 HTTP 标头中提取 cookie。如果您使用的是 JAX-WS,您可以使用BindingProvider.SESSION_MAINTAIN_PROPERTY以下命令启用会话感知:

Hello proxy = new HelloService().getHelloPort();
((BindingProvider)proxy).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY,true);
String result = proxy.getMessage();
System.out.println(result); 

If not, then try to find how to get and set the HTTP headers using your web services framework.

如果没有,那么尝试查找如何使用您的 Web 服务框架获取和设置 HTTP 标头。

回答by Abhishek Asthana

There are multiple ways you can extract the session id.

有多种方法可以提取会话 ID。

  • From the xml you have provided it seems the session id is in the xml response. If this is the case the you can use the method suggestedby olyvand extract the session id from the xml response.

  • As lakshmansuggest you could use groovy to parse the session id from the xml response.

  • 从您提供的 xml 来看,会话 ID 似乎在 xml 响应中。如果是这种情况下,你可以使用建议的方法通过olyv,并从XML响应中提取的会话ID。

  • 正如lakshman建议您可以使用 groovy 从 xml 响应中解析会话 ID。

The below code may be of help.

下面的代码可能会有所帮助。

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
log.info holder.getNodeValue("//sessionId")
log.info holder['//sessionId']

This code and its explanation is available at Are you a hot cup?blog.

此代码及其解释可在您是热杯吗?博客。

  • Another option using groovy is to extract the cookie from the header and store it in a property. The value of this property will be set in each request by creating a header property called cookieand assigning the session id to it. You'll have to verify the property name and value format by looking at the raw response/request.
  • 使用 groovy 的另一个选择是从标题中提取 cookie 并将其存储在属性中。此属性的值将在每个请求中通过创建一个名为的标头属性cookie并为其分配会话 ID 来设置。您必须通过查看原始响应/请求来验证属性名称和值格式。

try this code for example

试试这个代码

//in script assertion
String message_size = messageExchange.responseHeaders["session-id"] /or whatever if the cookie name

or

或者

def state = context.getProperty( com.eviware.soapui.model.testsuite.TestRunContext.HTTP_STATE_PROPERTY )

assert state != null : "Missing HttpState.. Try to set 'Maintain HTTP session' in test case options"

def cookies = state.cookies

Above code sample is from http://forum.soapui.org/viewtopic.php?t=3066#p10957

以上代码示例来自http://forum.soapui.org/viewtopic.php?t=3066#p10957

  • Lastly, there is a test case level option to maintain http session, if you select this option you wouldn't have to worry about extracting the session id. The soapUI guide on this says..
  • 最后,有一个测试用例级别的选项来维护 http 会话,如果您选择此选项,您就不必担心提取会话 ID。关于这个的soapUI指南说..

For example, soapUI uses this internally to store an HttpState object in the context when the "Maintain HTTP Session" option has been selected in the TestCase Options dialog.

例如,当在 TestCase Options 对话框中选择了“Maintain HTTP Session”选项时,soapUI 在内部使用它来在上下文中存储 HttpState 对象。

link to the above line: http://www.soapui.org/Functional-Testing/testcase-execution.html

链接到上述行:http: //www.soapui.org/Functional-Testing/testcase-execution.html

回答by diginoise

API description you have attached points out that all you need to do is to manage your cookies correctly.

您所附的 API 说明指出您需要做的就是正确管理您的 cookie。

Successful login response will have the cookie with session ID (also duplicated as a <sessionId>xxxx</sessionId>element in the response).

成功的登录响应将包含带有会话 ID 的 cookie(也作为<sessionId>xxxx</sessionId>响应中的一个元素复制)。

All you have to do is to include this cookie in all subsequent calls to this API.

您所要做的就是在对该 API 的所有后续调用中包含此 cookie。

Depending on your HTTP/SOAP client these things just need setting up and the client simply follows the contract of HTTP Cookie standard, so it receives cookies, stores them for as long as they are valid, and passes them with all subsequent requests made to the same URI.

根据您的 HTTP/SOAP 客户端,这些东西只需要设置,客户端只需遵循 HTTP Cookie 标准的约定,因此它接收 cookie,只要它们有效就将它们存储起来,然后将它们与所有后续请求一起传递给相同的 URI。

If you are using SoapUI, just add session management to your test case as per the picture below:

如果您使用的是SoapUI,只需按照下图将会话管理添加到您的测试用例中:

test case options

测试用例选项

If that is not enough and you want to pop the bonnet up and see what is what see this blog

如果这还不够,并且您想弹出引擎盖,看看是什么,请参阅此博客

HTH

HTH