Java 调用天气 Webservice 的小应用
废话不多说 直接贴代码 :
CityReq.java
Пакет com.weather; импорт javax.xml.bind.annotation.xmlelement; импорт javax.xml.bind.annotation.xmlrootelement; @xmlrootelement (name = "getweatherbycityname", namespace = "http://webxml.com.cn/"). public String getThecityName () {return TheCityName; } @Xmlelement (name = "thecityname", namespace = "http://webxml.com.cn/") public void setthecityname (String thecityName) {this.TheCityName = theCityName; }}Weatherwebservicetest.java
Пакет com.weather; импорт java.io.inputstream; import java.io.outputstream; импорт java.net.httpurlconnection; импорт java.net.url; импорт javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import.comloryssers.saloryssers.shiloryssersssersserssersserssers. javax.xml.soap.messagefactory; import javax.xml.soap.soapbody; импорт javax.xml.soap.soapconstants; импорт javax.xml.soap.soapenvelope; импорт javax.xml.soap.soapmessage; импорт org.w.dom.documentsemest. {public static void main (string [] args) {// todo Автогенерированный метод stub weather (); } static void weather () {System.out.println ("开始登陆 ..."); String wsdl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl"; System.out.println ("wsdl:"+wsdl); Httpurlconnection urlconn = null; InputStream ins = null; OutputStream ous = null; try {url u = new url (wsdl); urlConn = (httpurlconnection) u.openconcenection (); urlconn.setDoOutput (true); urlconn.setrequestmethod ("post"); urlconn.setrequestproperty ("content-type", "Application/SOAP+XML; charset = UTF-8"); //urlconn.setrequestproperty("content-type "," text/xml; charset = utf-8 "); // 发送数据 ous = urlConn.getOutputStream (); Document Document = DocumentBuilderFactory.newinStance (). NewDocumentBuilder (). NewDocument (); // 编组 Marshaller Marsh = jaxbContext.newinStance (CityReq.class) .createMarShaller (); CityReq xmlf = new CityReq (); xmlf.setThecityName ("北京"); //Jaxb.marshal(xmlf, New PrintWriter (System.out)); Marsh.marshal (XMLF, документ); // 创建 SOAPMessage 对象 SOAPMessage SOAPMessage = messageFactory.newinStance (SOAPConstants.SOAP_1_2_PROTOCOL) .CREATEMESSAGE (); Мыловое тело мыло = soapMessage.getSoApbody (); Soapbody.adddocument (документ); SOAPENVELOPE SOAPENVELOPE = SOAPMESSAGE.GETSOAPPART (). GetEnvelope (); Soapenvelope.RemovenamespedCedEclaration («env»); SOAPENVELOPE.AddnamespedEclaration ("SOAP12", "http://www.w3.org/2003/05/soap-envelope"); SOAPENVELOPE.AddnamespedEclaration ("xsi", "http://www.w3.org/2001/xmlschema-insstance"); SOAPENVELOPE.AddnamespedEclaration ("xsd", "http://www.w3.org/2001/xmlschema"); Soapenvelope.setPrefix ("SOAP12"); Soapenvelope.RemoveChild (Soapenvelope.getheader ()); Soapbody.setPrefix ("SOAP12"); // 发送数据 SoapMessage.WriteTo (ous); // soapMessage.writeTo (System.out); System.out.println (urlconn.getReponsecode ()); System.out.println (urlconn.getResponsemessage ()); // 接收数据 ins = urlConn.getInputStream (); // 接收的数据需要解组? StringBuffer respmsg = new StringBuffer (); байт [] байты = новый байт [1024*1024]; int a = -1; while ((a = ins.read (bytes))! =-1) {respmsg.append (новая строка (байты, 0, а)); } System.out.println (respmsg.length ()); System.out.println (respmsg); // 解组的方式 /* soapMessage responsemessage = messageFactory.newinStance (SOAPConstants.SOAP_1_2_PROTOCOL). CREATEMESSAGE (NULL, INS); Unmarshaller unmarsh = jaxbcontext.newinstance (cityresp.class). Createunmarshaller (); Jaxbelement <sityResp> desponse = unmarsh.unmarshal (responsemessage.getsoapbody (). ExtractContentAsDocument (), CityResp.class); CityResp uresp = desponse.getValue (); System.out.println (uresp.getResult ());*/ ous.close (); ins.close (); urlconn.disconnect (); } catch (Exception e) {e.printstackTrace (); }окончательно{ } } }感谢阅读 , 希望能帮助到大家 谢谢大家对本站的支持!