What is WebService:
I don't need to talk nonsense here. If you haven't been familiar with this knowledge, you can check it online first. Here I just want to talk about some of my impressive points:
WebService is based on the SOAP protocol. To be honest, I didn't understand this knowledge very well at the beginning. For a long timeCodeThinking in the process. I will introduce the weather forecast in detail in the following example. In short, all WebService requests and responses are based on the SOAP protocol, while the carrier of soap data transmission is XML. A detailed tutorial on the SOAP protocol is provided on W3C.
WSDL is the description language of WebService. It defines what the web service is doing, how it is done, and how it is queried. When verifying whether a WebService is useful, we usually choose to enter http: //… in the browser ://....... *? WSDL. If an XML file is displayed, we think it is useful, and vice versa. At the beginning of the project, I thought so naively, because it was demonstrated in the tutorial. However, this is not the case, just as the following calls the WSDL of the weather forecast. If you enter a http://fhs.6617.com/getweather.asmx in your browser? WSDL, It is not displayed, but it turns out that this WebService Service can be used (you can pass the http://fhs.6617.com/getweather.asmx? The local proxy class generated by the WSDL ). This is also a misunderstanding of WebService beginners. The reason why I cannot access the service through a browser may be that the Service publisher or the server imposes some restrictions, just as many servers on the Internet can access but ping different servers, and the service provider imposes some restrictions. (My guess)
How to Use WebService
Is it necessary to obtain the WSDL when WebService is called? Must a local proxy class be generated?
Before answering this question, I would like to introduce some resources on the Internet: There are many enthusiastic people who have collected many commonly used WebServices, such as http://lqixv.javaeye.com/blog/308407. The author provides three connections for each service listed in the List: endpoint disco WSDL. In fact, this is vaguely telling us three ways to call the WebService Service.
Just like in our project, when the client calls the telecom WebService to send upstream text messages, it uses the WSDL to generate a local proxy class. When receiving the status report, the line uses the endpoint method (the client does not need to generate a local proxy class, but only needs to know the endpoint address ).
Therefore, WSDL is very important, but it is not necessary to obtain the address and namespace information you need through APIs and standard documents.
How to call WSDL to generate a local proxy class:
There are many ways to generate a local proxy class through WSDL, either manually generated using the wsdl2java command, or through the axis2 plug-in of Eclipse, xfire plug-in, etc. However, through the project practice, I think it is best to generate it through the wsdl2java command of axis2, which saves a lot of trouble. For example, if you have a lot of WSDL files, they are connected to each other. In this way, errors may occur when you generate them using the Eclipse plug-in. Because it can only load one WSDL file at a time. At the early stage of the project, I suffered a lot from this problem .....
Axis2 is a commonly used WebService engine, you can go to the routing, axis2-1.4.1-war.zip file is used to publish WebService to the Web Container, there are a lot of axis2 tutorials on the Internet, not to mention here.
Weather forecast call example:
The following example is an example of calling weather forecast. Without using WSDL to generate a local proxy class, the method of directly accessing the service endpoint through an HTTP request is used:
Steps:
1. Use soap to request the WebService endpoint and retrieve the request results
2. parse the returned XML stream through dom4j to obtain the desired information.
Through this example, I believe that you will have a deeper understanding of the sentence "webserviice is based on the SOAP protocol". In addition, when using dom4j to parse the XML returned stream, encountered some trouble, need to introduce an additional jaxen-1.1.1.jar package, otherwiseProgramThe error ORG/jaxen/jaxenexception is reported. When XML contains a namespace, the positioning element must be written according to the XPath syntax.
Below is the classSource code:
Code
Import Java. Io. inputstream;
Import Java. Io. outputstream;
Import Java. Io. outputstreamwriter;
Import Java.net. url;
Import Java.net. urlconnection;
Import Java. util. hashmap;
Import Java. util. iterator;
Import Java. util. List;
Import Java. util. Map;
Import Org. dom4j. Document;
Import Org. dom4j. extends enthelper;
Import Org. dom4j. element;
Import Org. dom4j. Io. saxreader;
/**
* Calls WebService to obtain the weather forecast service.
* @ Author Qsw-myonlystar 09:59:45
*/
Public Class Weather {
/**
* Get the SOAP request header and replace the flag with the user's input symbol.
* @ Param City user input city name
* @ Return SOAP request to be sent to the server
*/
Private Static String getsoaprequest (string city ){
Stringbuilder sb = New Stringbuilder ();
SB. append ( " <? XML version = \ " 1.0 \ " Encoding = \ " UTF - 8 \ " ?> "
+ " <Soap: envelope xmlns: xsi = \ " HTTP: // Www.w3.org/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> " );
Return SB. tostring ();
}
/**
* The user sends the SOAP request to the server and returns the input stream returned by the server point.
* @ Param City name entered by the user
* @ Return The input stream returned by the server for the client to read
* @ Throws Exception
*/
Public Static Inputstream getsoapinputstream (string City) Throws Exception {
Try {
String soap = Getsoaprequest (city );
If (Soap = Null ){
Return Null ;
}
URL = New URL (
" 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 = New Outputstreamwriter (OS, " UTF-8 " );
OSW. Write (SOAP );
OSW. Flush ();
OSW. Close ();
Inputstream is = Conn. getinputstream ();
// System. Out. println (is. tostring ());
Return Is;
} Catch (Exception e ){
E. printstacktrace ();
Return Null ;
}
}
/**
* Parse the XML returned by the server through dom4j
* @ Param City name entered by the user
* @ Return String used, separated
*/
Public Static String getweather (string city ){
Document document = Null ;
Saxreader Reader = New Saxreader ();
String s = "" ;
Map = New Hashmap ();
Map. Put ( " Design " , " Http://WebXml.com.cn/ " );
Reader. getdocumentfactory (). setxpathnamespaceuris (MAP );
Try {
Inputstream is = Getsoapinputstream (city ); // Get input stream
Document = Reader. Read (is ); // Converts an input stream to a document
String t = Document. asxml ();
} Catch (Exception e ){
// Todo auto-generated Catch Block
E. 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 " ;
}
Return S;
}
/**
* Test functions
* @ Param ARGs
*/
Public Static Void Main (string ARGs []) {
Weather W = New Weather ();
System. Out. println (W. getweather ( " Tai'an " ));
}
}
In addition, we recommend a good website, http://www.webxml.com.cn/zh_cn/index.aspx. There are many good WebServices available for us to call.