Nonsense not much to say, direct sticker code:
Cityreq.java
PackageCom.weather;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name= "Getweatherbycityname", namespace= "http://WebXml.com.cn/") Public classCityreq {PrivateString Thecityname; PublicString Getthecityname () {returnThecityname; } @XmlElement (Name= "Thecityname", namespace= "http://WebXml.com.cn/") Public voidsetthecityname (String thecityname) { This. Thecityname =Thecityname; } }
Weatherwebservicetest.java
PackageCom.weather;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJavax.xml.bind.JAXBContext;ImportJavax.xml.bind.Marshaller;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.soap.MessageFactory;ImportJavax.xml.soap.SOAPBody;Importjavax.xml.soap.SOAPConstants;ImportJavax.xml.soap.SOAPEnvelope;ImportJavax.xml.soap.SOAPMessage;Importorg.w3c.dom.Document; Public classWeatherwebservicetest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubweather (); } Static voidweather () {System.out.println ("Start landing ..."); 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; Try{URL u=NewURL (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 "); //Send Dataous=Urlconn.getoutputstream (); Document Document=documentbuilderfactory.newinstance (). Newdocumentbuilder (). NewDocument (); //GroupMarshaller marsh=jaxbcontext.newinstance (cityreq.class). Createmarshaller (); Cityreq XMLF=NewCityreq (); Xmlf.setthecityname (Beijing); //Jaxb.marshal (XMLF, New PrintWriter (System.out));Marsh.marshal (XMLF, document); //Create a SoapMessage objectSoapMessage soapmessage=messagefactory.newinstance (Soapconstants.soap_1_2_protocol). CreateMessage (); Soapbody Soapbody=Soapmessage.getsoapbody (); Soapbody.adddocument (document); SoapEnvelope SoapEnvelope=Soapmessage.getsoappart (). Getenvelope (); Soapenvelope.removenamespacedeclaration ("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"); //Send DataSoapmessage.writeto (OUs); //Soapmessage.writeto (System.out);System.out.println (Urlconn.getresponsecode ()); System.out.println (Urlconn.getresponsemessage ()); //Receive Datains=Urlconn.getinputstream (); //The data you receive requires a solution group?StringBuffer respmsg=NewStringBuffer (); byte[] bytes=New byte[1024*1024]; intA=-1; while((A=ins.read (bytes))!=-1) {respmsg.append (NewString (bytes,0, a)); } System.out.println (Respmsg.length ()); System.out.println (RESPMSG); //How to solve a group /*soapmessage responsemessage=messagefactory.newinstance (soapconstants.soap_1_2_protocol). CreateMessage (NULL, INS); Unmarshaller unmarsh=jaxbcontext.newinstance (Cityresp.class). Createunmarshaller (); Jaxbelement<cityresp> reponse= 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 (); }finally{ } } }
Java calls for small applications of weather webservice