Java 如何从URL调用JAX-WS方法

Java 如何从URL调用JAX-WS方法,java,web-services,wsdl,jax-ws,Java,Web Services,Wsdl,Jax Ws,我有一个本地web服务,可以使用JAVA客户端调用它的方法 是否可以使用URL访问其方法? 我可以使用以下URL访问wsdl XML: 我想调用这样一个方法: 但我收到错误“Localhost未发送任何数据” 有办法做到这一点吗?据我所知,Jax ws使用POST来接收呼叫。您必须构建一个XML请求才能发布到您的URL。大概是这样的: POST /ws/hello HTTP/1.1 SOAPAction: "" Accept: text/xml, multipart/related, tex

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

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

我想调用这样一个方法:

但我收到错误“Localhost未发送任何数据”


有办法做到这一点吗?

据我所知,Jax ws使用POST来接收呼叫。您必须构建一个XML请求才能发布到您的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>
POST/ws/hello HTTP/1.1
SOAPAction:“
接受:text/xml、多部分/相关、text/html、image/gif、image/jpeg、*;q=.2,*/*;q=.2
内容类型:text/xml;字符集=utf-8
用户代理:Java/1.6.0_13
主机:localhost:9999
连接:保持活力
内容长度:224
测试

使用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();
   }