What is WebService:
This does not need me here nonsense, on-line information search a lot of, if you have not contacted this knowledge, you can first go online check. Here I just want to say a few more impressive points:
WebService is based on the SOAP protocol. To tell the truth, I didn't understand the knowledge at first. It also limits my thinking in the code process for a long time. I'll explain in detail in a later example of the weather forecast. In short, all webservice requests and responses are based on the SOAP protocol, while the carrier of SOAP transport data is XML. The introduction of the SOAP protocol has a very detailed tutorial on the World Series.
WSDL is the description language of WebService, which defines what the Web service does, how it is done, and what information is queried. When verifying that a webservice is useful, we usually choose to enter HTTP://.......*?WSDL in the browser. If an XML file is displayed, we think it is useful and vice versa. In the beginning of the project, I was so naïve to think, because the tutorial is so demonstrated. But this is not the case, as is the following call to the weather forecast WSDL. What if you entered http://fhs.6617.com/getweather.asmx in the browser? WSDL, it is not shown, but it turns out that this WebService service can be used (can you pass http://fhs.6617.com/getweather.asmx?). WSDL generates a local proxy class). This is also always a beginner webservice of a misunderstanding. It is not possible to access through the browser, perhaps because the service publisher, or the server has made some restrictions, as many servers on the Internet, we can access but ping different, the service has made some restrictions. (My guess)
How to use WebService
Is it necessary to get WSDL when calling WebService? Must you generate a local proxy class?
Before answering this question, I would like to introduce some resources on the Internet: There are a lot of enthusiastic people, collected a lot of commonly used webservice, such as: http://lqixv.javaeye.com/blog/308407. For each of the services listed, the authors provide three connections: Endpoint Disco WSDL. In fact, this is the three ways to tell us to call the WebService service.
Just like our project, when the client calls the telecom WebService uplink, it is implemented by generating a local proxy class through WSDL. When receiving status reports downstream, the endpoint method is used (the client does not need to generate a local proxy class, just need to know the endpoint address).
So WSDL is important, but not necessary, if you can get the information about the address and namespace you need through the API, specification document, etc.
How to invoke WSDL to generate a local proxy class:
There are many ways to generate local proxy classes through WSDL, either manually through the Wsdl2java command or through Eclipse's Axis2 plug-ins, Xfire plugins, and so on. But through the project practice, feel as far as possible through the Axis2 Wsdl2java command generation, will save a lot of trouble. For example, if you have a lot of WSDL files and they are interconnected, you will get an error when you build through the Eclipse plugin. Because it can only load a WSDL file at a moment. This problem at the beginning of the project, let me eat a lot of hardships .....
Axis2 is a more commonly used webservice engine that can be downloaded to http://ws.apache.org/axis2/, where the Axis2-1.4.1-bin.zip file contains all the jar files in Axis2, and the command tool, the Axis2-1.4.1-war.zip file is used to publish WebService to the Web container, there are many axis2 tutorials on the internet, which is no longer mentioned here.
Example of a weather call:
The following example is an example of invoking the weather forecast without using WSDL to generate a local proxy class, using the method of directly accessing the service endpoint over an HTTP request:
The steps are:
1, use SOAP to WebService endpoint request, retrieve the request result
2, through DOM4J parse the returned XML stream, get the information you want.
This example believes that you will have a deeper understanding of the phrase "Webserviice is based on the SOAP protocol", and in the process of using DOM4J to parse the return stream of XML, there are some problems that need to be introduced into the Jaxen-1.1.1.jar package, otherwise the program will report org/ Jaxen/jaxenexception's error. And when the XML contains namespaces, the positioning elements need to be written according to the XPath syntax.
The following is the source code for the class:
ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;ImportJava.net.URL;Importjava.net.URLConnection;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;Importorg.dom4j.Document;ImportOrg.dom4j.DocumentHelper;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;/*** Class action call WebService get Weather forecast service *@authorQsw-myonlystar 2010-1-13 a.m. 09:59:45*/ Public classWeather {/*** Get the SOAP request header and replace the glyph with the user's input symbol *@paramCity Users enter the name *@returnSOAP requests that the user will send to the server*/ Private Staticstring Getsoaprequest (String city) {StringBuilder sb=NewStringBuilder (); Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" + "<soap:envelope xmlns:xsi=\" http://www.w3.o Rg/2001/xmlschema-instance\ "" + "xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ "" + "xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">" + "<soap:Body> < ; Getweatherbycityname xmlns=\ "http://webxml.com.cn/\" > "+" <theCityName> "+ City+ "</theCityName> </getWeatherbyCityName>" + "</SOAP:BODY></SOAP:ENVELOPE&G t; "); returnsb.tostring (); } /*** The user sends the SOAP request to the server side and returns the input stream returned by the server point *@paramCity User input name *@returninput stream returned by server side for client to read *@throwsException*/ Public StaticInputStream Getsoapinputstream (String city)throwsException {Try{String Soap=getsoaprequest (city); if(Soap = =NULL) { return NULL; } URL URL=NewURL ("Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"); URLConnection Conn=url.openconnection (); Conn.setusecaches (false); Conn.setdoinput (true); Conn.setdooutput (true); Conn.setrequestproperty ("Content-length", integer.tostring (soap. Length ())); Conn.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); Conn.setrequestproperty ("SOAPAction", "Http://WebXml.com.cn/getWeatherbyCityName"); OutputStream OS=Conn.getoutputstream (); OutputStreamWriter OSW=NewOutputStreamWriter (OS, "Utf-8")); Osw.write (SOAP); Osw.flush (); Osw.close (); InputStream is=Conn.getinputstream (); //System.out.println (is.tostring ()); returnis ; } Catch(Exception e) {e.printstacktrace (); return NULL; } } /*** Parsing of XML returned by the server side via DOM4J *@paramCity User input name *@returnstring, Split*/ Public Staticstring GetWeather (String city) {Document document=NULL; Saxreader Reader=NewSaxreader (); String s=""; Map Map=NewHashMap (); Map.put ("Design", "http://WebXml.com.cn/"); Reader.getdocumentfactory (). Setxpathnamespaceuris (map); Try{InputStream is= Getsoapinputstream (city);//Get input streamDocument=reader.read (IS);//translate input into documentString t=Document.asxml (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } List Nodes= Document.selectnodes ("//design:string"); for(Iterator it =nodes.iterator (); It.hasnext ();) {Element Elm=(Element) it.next (); String text=Elm.gettext (); //System.out.println ("fsffs" +text);S=s+elm.gettext () + "\ n"; } returns; } /*** Test function *@paramargs*/ Public Static voidMain (String args[]) {Weather W=NewWeather (); System.out.println (W.getweather (Taian)); }}
In addition, we recommend a better website, http://www.webxml.com.cn/zh_cn/index.aspx. A lot of good webservice are available for us to call.
Some summaries of webservice use