Java 调用天气 webservice 的小应用
废话不多说, : :
CityReq.Java
Package com.weather; Importer javax.xml.bind.annotation.xmlelement; Importer javax.xml.bind.annotation.xmlrootelement; @xmlrootelement (name = "getweatherbycityName", namespace = "http://webxml.com.cn/") Classe publique cityreq {privé public String getTheCityName () {return theCityName; } @Xmlelement (name = "theCityName", namespace = "http://webxml.com.cn/") public void setTheCityName (String theCityName) {this.thecityName = theCityName; }}WeatherWebserviceTest.java
package com.weather; import java.io.inputstream; import java.io.outputstream; import java.net.httpurlconnection; import java.net.url; import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; javax.xml.soap.messagefactory; import javax.xml.soap.soapbody; import javax.xml.soap.soapconstants; import javax.xml.soap.soapenvelope; import javax.xml.soap.soapMessage; Import Org.w3c. Main (String [] args) {// TODO Méthode générée automatique 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; essayez {url u = new URL (wsdl); urlConn = (httpurlConnection) u.openconnection (); 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, document); // 创建 SOAPMessage 对象 SoapMessage SoapMessage = MessageFactory.NewInstance (SoapConstants.SoAP_1_2_ProtoCol) .CreateMessage (); Soapbody soapbody = soapMessage.getSoapBody (); SoapBody.AddDocument (document); SoapenVelope SoapenVeelope = SoapMessage.getSoAppart (). GetEnvelope (); Soapenvelope.ReMoveNamesPaceCatedEClaration ("Env"); Soapenvelope.AddNamesPaceDeClaration ("SOAP12", "http://www.w3.org/2003/05/soap-envelope"); Soapenvelope.addNaMesPacedEClaration ("XSI", "http://www.w3.org/2001/xmlschema-instance"); SoapenVelope.addNaMesPacedEclartation ("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.getResponSECODE ()); System.out.println (urlConn.getResponseMessage ()); // 接收数据 ins = urlConn.getInputStream (); // 接收的数据需要解组? StringBuffer respmsg = new StringBuffer (); octet [] octets = nouveau octet [1024 * 1024]; int a = -1; while ((a = ins.read (bytes))! = - 1) {respmsg.append (nouvelle chaîne (octets, 0, a)); } 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 <tityResp> Réponse = unmarsh.unmarshal (resensEMessage.getSoapBody (). ExtractContentaSDocument (), CityResp.Class); CityResp uResp = reponce.getValue (); System.out.println (uResp.getResult ()); * / ou.close (); ins.close (); UrlConn.Disconnect (); } catch (exception e) {e.printStackTrace (); }enfin{ } } }感谢阅读 , 希望能帮助到大家 , 谢谢大家对本站的支持!