在 Java 中通过 HTTP 发送 SOAP 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330622/
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-10-30 05:55:12  来源:igfitidea点击:

sending a SOAP message via HTTP in java

javaweb-servicessoap

提问by aherlambang

I have the following code:

我有以下代码:

  String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + 
        "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\" " + 
          "xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" " + 
          "xmlns:ns1=\"http://org.apache.axis2/xsd\"  " + 
          "xmlns:ns=\"http://tfc\" " +
          "xmlns:wsaw=\"http://www.w3.org/2006/05/addressing/wsdl\" " +
          "xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\"  " +
          "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" +
          "xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"  " +
          "xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"  " +
          "xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\"  " +
          "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  " +
          "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" > " +
          "<SOAP-ENV:Body>" +
              "<ns:CalFare xmlns:ns=\"http://tfc\">" +
              "<ns:nonairport>1</ns:nonairport>" +
              "<ns:distance>20</ns:distance>" +
              "</ns:CalFare>" +
          "</SOAP-ENV:Body>" +
          "</SOAP-ENV:Envelope>";

          //Create socket
          String hostname = "128.196.239.112";
          int port = 8080;
          InetAddress  addr = InetAddress.getByName(hostname);
          Socket sock = new Socket(addr, port);

          //Send header
          String path = "/LocatorzTaxiFare/services/Calculator.CalculatorHttpSoap11Endpoint/";
          BufferedWriter  wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
          // You can use "UTF8" for compatibility with the Microsoft virtual machine.
          wr.write("POST " + path + " HTTP/1.0\r\n");
          wr.write("Host: 128.196.239.112\r\n");
          wr.write("Content-Length: " + xmldata.length() + "\r\n");
          wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
          wr.write("\r\n");

          //Send data
          wr.write(xmldata);
          wr.flush();

          // Response
          BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
          String line;
          while((line = rd.readLine()) != null)
        System.out.println(line);
        } catch (Exception e) {
          e.printStackTrace();
        }

Now it's giving me an internal server error with the following response:

现在它给我一个内部服务器错误,响应如下:

<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://www.w3.org/2005/08/addressing/soap/fault</wsa:Action></soapenv:Header><soapenv:Body><soapenv:Fault><faultcode></faultcode><faultstring>com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character 'x' (code 120) excepted space, or '>' or "/>"&#xd;
 at [row,col {unknown-source}]: [1,390]</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>

Here's a linkto the WSDL

这是 WSDL的链接

回答by Will Hartung

At a glance, it looks like the XML that you're sending it is invalid. The XML processor found an 'x' when it was looking for a space, a '>' or a '/>'. So, fix your payload.

乍一看,您发送的 XML 似乎无效。XML 处理器在查找空格、“>”或“/>”时发现了一个“x”。所以,修复你的有效载荷。

Yup...here it is:

是的……这里是:

 "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" +
 "xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"  " +

That first line is wrong, you need to add the trailing space (like in the second line).

第一行是错误的,您需要添加尾随空格(如第二行)。

Mind, it helps to read the error messages. This is exactly what it said was wrong. No real magic here.

请注意,它有助于阅读错误消息。这正是它所说的错误。这里没有真正的魔法。