0

我必须在我的应用程序发送到另一个应用程序的请求的 cookie 中添加信息,但似乎没有正确添加。

当我使用 WireShark 检查请求时,我在标头中看到两个 Cookie 标头:

POST /service HTTP/1.1
Accept-Encoding: gzip
Cookie: iam=**************************
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: text/xml; charset=utf-8
Content-Length: 128393
Host: host-dev:9999
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_271)
Cookie: JSESSIONID=***********************
Authorization: Basic **************************

(我已经更改了一些信息)

在我的代码中,我有这个:

@Service
public class ESignatureSoapConnector extends WebServiceGatewaySupport {

    private ObjectFactory objectFactory;

    @Autowired
    public ESignatureSoapConnector(ESignatureMarshaller marshaller, ConfigurationProperties configurationProperties) throws Exception { 
        this.setMarshaller(marshaller);
        this.setUnmarshaller(marshaller);
        this.setDefaultUri(configurationProperties.getBaseUrl());
        this.setMessageSender(buildMessageSender(configurationProperties.getUsername(), configurationProperties.getPassword()));

        this.objectFactory = new ObjectFactory();
    }

    public ESignatureResponse signDocument(MTOMFile file, String iamCookieValue) {
        ESignature request = new ESignature();
        request.setInputDocument(file);
        JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
                .marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
                        TransportContext context = TransportContextHolder.getTransportContext();
                        HttpComponentsConnection connection = (HttpComponentsConnection) context.getConnection();
                        HttpPost post = connection.getHttpPost();
                        post.addHeader("Cookie", "iam=" + iamCookieValue);
                    }
                });
        return response.getValue();
    }

    private WebServiceMessageSender buildMessageSender(String username, String password) throws Exception {
        ...
    }

}

我假设我设置 cookie 的方式不正确,但我找不到正确的方法。每个请求的 cookie 值都不同,这是一个肥皂请求,我在 Spring 中工作

4

1 回答 1

0

我们找到的解决方案:

JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
                .marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
                        HttpClient httpClient = ((HttpComponentsMessageSender) getWebServiceTemplate().getMessageSenders()[0]).getHttpClient();
                        BasicClientCookie iamCookie = new BasicClientCookie(iamConfigurationProperties.getCookieName(), iamCookieValue);
                        iamCookie.setDomain(iamConfigurationProperties.getCookieDomain());
                        iamCookie.setPath(iamConfigurationProperties.getCookiePath());
                        ((DefaultHttpClient) httpClient).getCookieStore().addCookie(iamCookie);
                    }
                });
于 2021-04-16T09:28:37.580 回答