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(); }最后{ } } }感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!,谢谢大家对本站的支持!