Java 调用天气 Web Service 的小应用
废话不多说, 直接贴代码:
CityReq.java
package com.weather;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="getWeatherbyCityName",namespace="http://WebXml.com.cn/")public class CityReq { private String theCityName; public String getThecityName () {return thecityName; } @Xmlelement (name = "thecityName", namespace = "http://webxml.com.cn/") public void setThecityName (string thecityName) {this.thecityName = theCityName; }}WeatherWebServiceTest.java
pacote com.weather; importar java.io.inputStream; importar java.io.outputStream; importar java.net.httpurlConnection; importar java.net.url; importar javax.xml.bind.jaxbContext; import javax.xml.bind.marhnaller; javax.xml.parsers.documentBuilderFactory; importar javax.xml.soap.messageFactory; importar javax.xml.soap.soapbody; import javax.xml.soap.soapCants; org.w3c.dom.document; public class WeatherwebServicetest {public static void main (string [] args) {// TODO Method Auto-Generated 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; tente {url u = novo url (wsdl); urlconn = (httpurlConnection) u.openconnection (); urlconn.setdoOutput (true); urlconn.setRequestMethod ("post"); urlconn.setRequestProperty ("conteúdo-tipo", "aplicativo/sabão+xml; charset = utf-8"); //urlconn.setRequestProperty("content-type "," text/xml; charset = utf-8 "); // 发送数据 ous = urlconn.getOutputStream (); Documento do documento = 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, documento); // 创建 SoapMessage 对象 SoapMessage SoapMessage = MessageFactory.NewInstance (SoapConstants.soap_1_2_protocol) .createmessage (); SoapBody SoapBody = SoapMessage.getSoapBody (); SoapBody.AddDocument (documento); SOAPENVELOPE SOAPENVELOPE = SOAPMESSAGE.GETSOAPT (). GetEnvelope (); SOAPENVELOPE.REMEMOVENESSPACECLARATION ("ENV"); SOAPENVELOPE.ADDNAMESPACECLARATION ("SOAP12", "http://www.w3.org/2003/05/soap-envelope"); SOAPENVELOPE.ADDNAMESPACECLARATION ("XSI", "http://www.w3.org/2001/xmlschema-instance"); SOAPENVELOPE.ADDNAMESPACECLARATION ("XSD", "http://www.w3.org/2001/xmlschema"); SOAPENVELOPE.SETPREFIX ("SOAP12"); sabonenvelope.removeChild (SOAPENVELOPE.GETHEADER ()); SoapBody.SetPrefix ("SOAP12"); // 发送数据 SoapMessage.WritEto (OUS); // SOAPMESSAGE.WRITETO (System.out); System.out.println (urlconn.getResponsecode ()); System.out.println (urlconn.getResponsemessage ()); // 接收数据 ins = urlconn.getInputStream (); // 接收的数据需要解组? Stringbuffer respmsg = new StringBuffer (); byte [] bytes = novo byte [1024*1024]; int a = -1; while ((a = ins.read (bytes))! =-1) {respmsg.append (new string (bytes, 0, a)); } System.out.println (respmsg.length ()); System.out.println (respmSG); // 解组的方式 /* SoapMessage Responsemessage = MessageFactory.NewInstance (SoapConstants.soap_1_2_protocol) .Createmessage (NULL, INS); UNMARSHALLER UNSARSH = JAXBCONTEXT.NEWINSTANCE (CityResp.class) .CreateUnMarshaller (); JAXBELEAMENTO <CTILERESP> REPONSE = UMARSH.UNMARSHAL (Responsemessage.getSoapBody (). ExtraCTContentasDocument (), CityResp.class); CityResp uresp = reponsonse.getValue (); System.out.println (uresp.getResult ());*/ ous.close (); ins.close (); urlconn.disconnect (); } catch (Exceção e) {e.printStackTrace (); }finalmente{ } } }感谢阅读 , 希望能帮助到大家 , 谢谢大家对本站的支持!