3

我现在正在研究 Java Web 服务客户端。但我面临一个大问题。我的问题是:我通过 Eclipse 的 Web 服务客户端生成器生成了 Web 服务客户端。它通过 Apache Axis2 生成 Web 服务。生成了我的 Web 服务 java 代码,我成功地将它与不需要安全标头的 wsdl 一起使用。但是我不能将它与需要安全标头的 Web 服务一起使用。

我尝试使用 fiddler2 来获取我的请求。但提琴手没有捕获任何与我的请求相关的数据包。但是当我打印我创建的请求并将这个xml与soapui一起使用时,操作成功执行。

增加超时设置不起作用。

我能做些什么?

我的 xml 和 java 错误如下:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">
         <wsse:UsernameToken>
            <wsse:Username>username</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
      <wsse:ReplyTo xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:Address>http://www.w3.org/2005/08/addressing/anonymous</wsse:Address>
      </wsse:ReplyTo>
      <wsse:MessageID xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">urn:uuid:123</wsse:MessageID>
      <wsse:Action xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1">http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgula</wsse:Action>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:TcKimlikNoIleKisiSorgula xmlns:ns1="http://www.saglik.gov.tr/KPS/2011">
         <ns1:tcNo>155***58</ns1:tcNo>
      </ns1:TcKimlikNoIleKisiSorgula>
   </soapenv:Body>
</soapenv:Envelope>

和错误信息:

[INFO] Unable to sendViaPost to url[https://kps.saglik.gov.tr/services/kpsservices.svc]
java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
    at sun.security.ssl.InputRecord.read(InputRecord.java:480)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
    at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
    at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1413)
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621)
    at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193)
    at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
    at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
    at org.apache.axis2.description.OutInAxisOperationClient$NonBlockingInvocationWorker.run(OutInAxisOperation.java:446)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

我的代码在下面

public static void main(String[] args){
ServiceStub stub = new ServiceStub();
    ServiceClient client = stub._getServiceClient();
    client.addHeader(createSecurityHeader("username", "password"));

    OMNamespaceImpl wsseNS = new OMNamespaceImpl(WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
    OMFactory factory = new SOAP11Factory();
    OMElementImpl replyToElement;
    OMElementImpl addressElement;
    OMElementImpl messageIDElement;       OMElementImpl actionElement;

    replyToElement = new OMElementImpl("ReplyTo", wsseNS, factory);

    addressElement = new OMElementImpl("Address", wsseNS, replyToElement, factory);
    addressElement.setText("http://www.w3.org/2005/08/addressing/anonymous");

    client.addHeader(replyToElement);
    messageIDElement = new OMElementImpl("MessageID", wsseNS, factory);
    messageIDElement.setText("urn:uuid:"+123);
    client.addHeader(messageIDElement);
    actionElement = new OMElementImpl("Action", wsseNS, factory);
    actionElement.addAttribute("mustUnderstand", "1", null);
    actionElement.setText("http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgula");
    client.addHeader(actionElement);
    Options options = client.getOptions();

    options.setProperty(HTTPConstants.CHUNKED, "false");
    options.setProperty(HTTPConstants.HTTP_PROTOCOL_VERSION, HTTPConstants.HEADER_PROTOCOL_11);

    TcKimlikNoIleKisiSorgula kisi = new TcKimlikNoIleKisiSorgula();
    kisi.setTcNo(155*****58L);//require an long
    stub.starttcKimlikNoIleKisiSorgula(kisi, callback);
}


public static OMElement createSecurityHeader(String username, String password)
{
        OMNamespaceImpl wsseNS = new OMNamespaceImpl(WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
        OMFactory factory = new SOAP11Factory();
        OMElementImpl securityHeader;
        OMElementImpl usernameTokenElement;
        OMElementImpl usernameElement;
        OMElementImpl passwordElement;

        // create the Security header block
        securityHeader = new OMElementImpl("Security", wsseNS, factory);
        securityHeader.addAttribute("mustUnderstand", "1", null);

        // nest the UsernameToken in the Security header
        usernameTokenElement = new OMElementImpl(WSConstants.USERNAME_TOKEN_LN, wsseNS, securityHeader, factory);

        // nest the Username and Password elements
        usernameElement = new OMElementImpl(WSConstants.USERNAME_LN, wsseNS, usernameTokenElement, factory);
        usernameElement.setText(username);

        passwordElement = new OMElementImpl(WSConstants.PASSWORD_LN, wsseNS, usernameTokenElement, factory);
        passwordElement.setText(password);
        passwordElement.addAttribute(WSConstants.PASSWORD_TYPE_ATTR,
        WSConstants.PASSWORD_TEXT, null);

        return securityHeader;
    }
4

1 回答 1

1

我遇到了同样的问题(我在 wso2 esb 环境中使用axis2 web-services)。

我通过提供额外的超时配置克服了这个问题:

<parameter name="SO_TIMEOUT">some_integer_value</parameter>
<parameter name="CONNECTION_TIMEOUT">some_integer_value</parameter>

这也可以在Options对象中指定:

options.setProperty(HTTPConstants.SO_TIMEOUT, new Integer(timeOutInMilliSeconds));
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(timeOutInMilliSeconds));

我将值 60000 用于超时,因为我的 Web 服务预计将使用 DB 执行多项操作。

于 2014-10-29T09:57:36.077 回答