Java calls for small applications of weather webservice

Source: Internet
Author: User
Tags soap wsdl

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.