Java調用天氣網絡服務的小應用
廢話不多說,直接貼代碼::
Citesreq.Java
軟件包com.weather; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement; @xmlrootelement(name =“ getweatherbycityname” public String getThecityName(){return thecityName; } @xmlelement(name =“ thecityName”,namespace =“ http://webxml.com.cn/”)public void setThecityName(string thecityName){this.thecityName = thecityName = thecityName; }}}weatherwebserviceTest.java
軟件包com.weather; import java.io.inputStream; import java.io.outputstream; import java.net.httpurlConnection; import java.net.url; import javax.xml.bind.jaxbcont. javax.xml.parsers.DocumentBuilderFactory;import 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.dom.document;公共類WeatherwebServicEtest {public static void main(string [] args){// todo自動生成的方法stub weather(); } static void weather(){system.out.println(“開始登陸...”);字符串wsdl =“ http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl”; system.out.println(“ wsdl:”+wsdl); httpurlconnection urlConn = null; inputstream ins = null; outputStream us = null;嘗試{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“); //發送數據US= urlConn.getOutputStream();文檔= documentBuilderFactory.NewInstance()。 newDocumentBuilder()。 newDocument(); //編組馬歇爾·馬什(Marshaller Marsh)= jaxbcontext.newinstance(citesreq.class).createmarshaller(); Citesreq 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.addocument(文檔); SOAPENVELOPE SOAPENVELOPE = SOAPMESSAGE.GEGSOAPPARP()。 getEnvelope(); soapenvelope.removenamespacedeclaration(“ env”); soapenvelope.addnamespacedeclaration(“ SOAP12”,“ http://www.w3.33.org/2003/05/soap-envelope”); soapenvelope.addnamespacedeclaration(“ xsi”,“ http://www.w3.33.org/2001/xmlschema-instance”); soapenvelope.addnamespacedeclaration(“ 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();字節[]字節=新字節[1024*1024]; int a = -1; while(((a = ins.read(bytes))!= - 1){despmsg.append(new String(bytes,0,a)); } system.out.println(despmsg.length()); system.out.println(respmsg); //解組的方式 /* soapMessage eversemessage = messageFactory.NewInstance(soapConstants.soap_1_2_protocol).createMessage(null,ins); unmarshaller unmarsh = jaxbcontext.newinstance(cityresp.class).createunmarshaller(); jaxbelement <CityResp> reponse = unmarsh.unmarshal(expresseMessage.getSoapbody()。extractContentasDocument(),cityresp.class); cityResp uresp = reponse.getValue(); system.out.println(uresp.getResult());*/ ous.close(); ins.close(); urlconn.disconnect(); } catch(異常E){e.printstacktrace(); }最後{ } } }感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持! ,謝謝大家對本站的支持!