Java invoke small application of weather webservice
No more nonsense to say, direct paste code:
Cityreq.java
Package com.weather;
Import javax.xml.bind.annotation.XmlElement;
Import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name= "Getweatherbycityname", namespace= "http://WebXml.com.cn/") public
class Cityreq {
Private String thecityname;
Public String Getthecityname () {return
thecityname;
}
@XmlElement (name= "Thecityname", namespace= "http://WebXml.com.cn/") public
void Setthecityname (String Thecityname) {
this.thecityname = thecityname;
}
}
Weatherwebservicetest.java
Package 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;
Import 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;
public class Weatherwebservicetest {public static void main (string[] args) {//TODO auto-generated stub
Weather ();
static void Weather () {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=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 ");
Send data Ous=urlconn.getoutputstream ();
Document document=documentbuilderfactory.newinstance (). Newdocumentbuilder (). NewDocument ();
Grouping Marshaller marsh=jaxbcontext.newinstance (cityreq.class). Createmarshaller ();
Cityreq xmlf=new cityreq ();
Xmlf.setthecityname ("Beijing");
Jaxb.marshal (XMLF, New PrintWriter (System.out));
Marsh.marshal (XMLF, document); Create the SoapMessage object SoapMessage 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 data Soapmessage.writeto (OUs);
Soapmessage.writeto (System.out);
System.out.println (Urlconn.getresponsecode ());
System.out.println (Urlconn.getresponsemessage ());
Receive data Ins=urlconn.getinputstream ();
The data received needs to be solved?
StringBuffer respmsg=new StringBuffer ();
Byte[] Bytes=new byte[1024*1024];
int a=-1;
while ((A=ins.read (bytes))!=-1) {respmsg.append (new String (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{}}}
Thank you for reading, I hope to help you, thank you for your support for this site!