Java 调用天气 Servis Webse 的小应用
废话不多说, : :
Cityreq.java
Paket com.weather; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement (name = "getWeatherByCityName", namespace = "http://weBxml.com.c.cne"), namespace = "http://weBxml.com.cne"), namespace = "http://webxml.com.cne"), namespace = "http://webxml.com. public string getThecityName () {return thecityName; } @Xmlelement (name = "thecityName", namespace = "http://webxml.com.cn/") public void setTheCityName (String thecityName) {this.thecityName = thecityName; }}Weatherwebservicetest.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.xml.bind.marshaller; javax.xml.parsers.documentbuilderfactory; import javax.xml.soap.messageFactory; import javax.xml.soap.soapbody; import javax.xml.soap.soapconstants; import javax.xml.soap.soapenvelop.soapconstants; org.w3c.dom.document; kelas publik weatherwebservicetest {public static void main (string [] args) {// TODO Metode yang dihasilkan secara otomatis Stub weather (); } static void cuaca () {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; coba {url u = url baru (wsdl); urlconn = (httpurlconnection) u.openconnection (); urlconn.setDoOutput (true); urlconn.setRequestMethod ("post"); urlconn.setRequestProperty ("tipe konten", "Aplikasi/SOAP+XML; charset = UTF-8"); //urlconn.setRequestProperty("Content-pePe "," Text/XML; Charset = UTF-8 "); // 发送数据 ous = urlconn.getoutputStream (); Dokumen dokumen = DocumentBuilderFactory.NewInstance (). NewDocumentBuilder (). NewDocument (); // 编组 Marshaller Marsh = jaxbcontext.newinstance (CityREQ.Class) .createMarshaller (); Cityreq xmlf = cityreq baru (); xmlf.setTheCityName ("北京"); //Jaxb.marshal(xmlf, printwriter baru (System.out)); Marsh.Marshal (XMLF, Dokumen); // 创建 SoapMessage 对象 SoapMessage SoapMessage = MessageFactory.NewInstance (soapconstants.soap_1_2_protocol) .createMessage (); Soapbody Soapbody = SoapMessage.getSoapbody (); Soapbody.adddocument (dokumen); SoapenVelope SoapenVelope = soapMessage.getSoAppart (). GetEnvelope (); Soapenvelope.romovenamespacedEclaration ("Env"); Soapenvelope.addnamespacedEclaration ("Soap12", "http://www.w3.org/2003/05/soap-envelope"); Soapenvelope.addnamespacedEclaration ("xsi", "http://www.w3.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 = stringBuffer baru (); byte [] bytes = byte baru [1024*1024]; int a = -1; while ((a = ins.read (bytes))! =-1) {respmsg.append (string baru (byte, 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> repons = unmarsh.unmarshal (responseMessage.getSoapbody (). ExtractContentAsdocument (), CityResp.class); CityResp uresp = reponse.getValue (); System.out.println (uresp.getResult ());*/ ous.close (); INS.CLOSE (); urlconn.disconnect (); } catch (Exception e) {E.PrintStackTrace (); }Akhirnya{ } } }感谢阅读 , 希望能帮助到大家 , 谢谢大家对本站的支持!