【发布时间】:2014-07-06 21:30:14
【问题描述】:
我正在尝试使用 NWS 肥皂服务器来获取经纬度预测。我尝试了几种方法,我选择的一种方法使用 javax.xml.soap。当我尝试时,jax-ws 方法立即失败:wsimport -keep http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl。我收到一个错误,表明它无法处理编码。这是我现在正在研究的来源:
package soapexample;
import javax.xml.soap.*;
public class SoapExample {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.weather.gov/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi", serverURI);
/*
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("NDFDgenByDay", "xsi");
soapBody.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("startDate", "xsi");
soapBodyElem1.addTextNode("2014-07-06");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("numDays", "xsi");
soapBodyElem2.addTextNode("3");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Unit", "xsi");
soapBodyElem3.addTextNode("e");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("format", "xsi");
soapBodyElem4.addTextNode("24 hourly");
SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("latitude", "xsi");
soapBodyElem5.addTextNode("38.99");
SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("longitude", "xsi");
soapBodyElem6.addTextNode("-77.01");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
`
它几乎可以工作,我得到了这样的回应:
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type="xsd:string">SERVER</faultcode>
<faultactor xsi:type="xsd:string"></faultactor>
<faultstring xsi:type="xsd:string">format needs to be either 24 hourly or 12 hourly</faultstring>
<detail xsi:type="xsd:string">input format was "-77.01"</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</code></pre>
此时我迷路了。我找不到我的请求的问题。感谢您的帮助。
【问题讨论】: