gpt4 book ai didi

java - 在 Java 中通过 HTTP 发送 SOAP 消息

转载 作者:行者123 更新时间:2023-11-29 08:16:41 25 4
gpt4 key购买 nike

我有以下代码:

  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>

这是一个 link到 WSDL

最佳答案

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

是的...在这里:

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

第一行是错误的,你需要添加尾随空格(就像第二行一样)。

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

关于java - 在 Java 中通过 HTTP 发送 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4330622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com