【问题标题】:How to call JAX-WS method from URL如何从 URL 调用 JAX-WS 方法
【发布时间】:2016-07-07 15:39:49
【问题描述】:

我有一个本地 Web 服务,我可以使用 JAVA 客户端调用它的方法。

是否可以使用 URL 访问其方法? 我可以使用这个 URL 访问 wsdl XML:

http://localhost:9999/ws/hello?wsdl

我想调用这样的方法:

http://localhost:9999/ws/hello/getHelloWorldAsString?name=test

但我收到错误“本地主机没有发送任何数据”。

有没有办法做到这一点?

【问题讨论】:

    标签: java web-services wsdl jax-ws


    【解决方案1】:

    据我所知,Jax-ws 使用 POST 来接听电话。您必须构建一个 XML 请求以 POST 到您的 URL。像这样的:

    POST /ws/hello HTTP/1.1
    SOAPAction: ""
    Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2,   */*; q=.2
    Content-Type: text/xml; charset=utf-8
    User-Agent: Java/1.6.0_13
    Host: localhost:9999
    Connection: keep-alive
    Content-Length: 224
    
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/">
                <arg0>test</arg0>
            </ns2:getHelloWorldAsString>
        </S:Body>
    </S:Envelope>
    

    【讨论】:

      【解决方案2】:

      使用 java.net.Url 和 HttpURLConnection 或 HTTPSURLConnection

      看样例

          URL url = new URL("http://yourwebservices.soap.wsdl");
          HttpURLConnection connectionWS = (HttpURLConnection) ur.openConnection();
          //not forget this
          connectionWS.setDoOutput(true);
          connectionWS.setDoInput(true);
          connectionWS.setRequestMethod("POST");
          connectionMinervaWS.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
      
         StringBuilder envelopeSoapRequest = new StringBuilder()
         //make the xml request
      
         //now you send to service
         OutputStreamWriter osw = new OutputStreamWriter( connectionWS.getOutputStream() );
         osw.write( envelopeSoapRequest.toString() );
         osw.flush();
      
         //now you can take response
         BufferedReader wsReader = null;
         StringBuilder envelopeSoapResponse = new StringBuilder();
         wsReader = new BufferedReader(new InputStreamReader( 
         connectionWS.getInputStream(), StandardCharsets.UTF_8 ));
         String line = wsReader.readLine();
      
         while (line != null) {
            envelopeSoapResponse.append( line );
            line = wsReader.readLine();
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        • 2012-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多