Java 调用天气 Webservice 的小应用
废话不多说, 直接贴代码 :
CityReq.java
Paket com.weather; import javax.xml.bind.annotation.xmlelement; import 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; }}Wetterwebservicetest.java
Paket 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.Bind.MmarShaller; javax.xml.soap.messagefactory; import javax.xml.soap.soapbody; import javax.xml.soap.soapconstants; import javax.xml.soap.soapenvelope; import Javax.xml.soap.soapmessage; void main (String [] args) {// Todo automatisch generierte Methode 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; Versuchen Sie {url u = new URL (WSDL); urlConn = (httpurlConnection) u.OpenConnection (); urlconn.setDooutput (true); urlconn.setRequestMethod ("post"); urlconn.setRequestProperty ("Content-Typ", "Anwendung/SOAP+XML; CharSet = UTF-8"); //urlconn.setRequestProperty("Content-Typ "," 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, neuer Pressewriter (System.out)); Marsh.Marshal (XMLF, Dokument); // 创建 SoapMessage 对象 SoapMessage soapMessage = messageFactory.Newinstance (SOAPCONSTANTS.SOAP_1_2_PROTOCOL) .CreateMessage (); SOAPBODY SOAPBODY = SOAPMESSAGE.getSoapbody (); Soapbody.Adddocument (Dokument); SoapEnelope SoapEnvelope = soapMessage.getSoAppart (). GetEnvelope (); SoapEnelope.RemoveNameSpaceClaration ("Env"); Soapenvelope.AddnamePaceClaration ("SOAP12", "http://www.w3.org/2003/05/soap-envelope"); Soapenvelope.AddnamePaceClaration ("XSI", "http://www.w3.org/2001/xmlschema-instance"); Soapenvelope.AddnamePaceClaration ("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 ESPMSG = new StringBuffer (); byte [] bytes = neues byte [1024*1024]; int a = -1; while ((a = Ins.Read (Bytes))! } System.out.println (respmsg.length ()); System.out.println (ESPMSG); // 解组的方式 /* soapMessage reponemessage = messageFactory.Newinstance (SoapConstants.soap_1_2_Protocol) .CreateMessage (NULL, INS); UNMARSHALLER UNMARSH = JAXBCONTEXT.NEWINSTANCE (CityResp.Class) .CreateUnmarshaller (); JaxBelement <CityResp> referonse = unmarsh.unmarshal (responseMessage.getSoapbody (). ExtractContentasDocument (), CityResp.Class); CityResp uresp = reponse.getValue (); System.out.println (uresp.getResult ());*/ ouS.close (); Ins.CLOSE (); urlconn.disconnect (); } catch (Ausnahme e) {e.printstacktrace (); }Endlich{ } } }感谢阅读 , 希望能帮助到大家 , 谢谢大家对本站的支持! 谢谢大家对本站的支持!