时间:2010-12-02 00:39:50

标签: java web-services soap

我有以下代码:

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

现在它给我一个内部服务器错误,并带有以下响应:

<?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>

这是WSDL的link

1 个答案:

答案 0 :(得分:5)

乍一看,您发送的XML似乎无效。 XML处理器在寻找空格时发现'x','&gt;'或者'/&gt;'。所以,修复你的有效载荷。

是的......这是:

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

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

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