Java frequently invoked method of WebService interface __java

Source: Internet
Author: User
Tags soap web services wsdl

WebService is a web-based service that implements remote invocation by using the SOAP protocol to implement a WebService and Cross-platform platform that receives and responds to some kind of request from an external system. WebService uses HTTP protocol to transmit data, uses XML format to encapsulate data, SOAP protocol =http protocol +xml data format. The main solution is not to different systems or to invoke the interface returned by the processing data items of a distributed deployment.

Recently, in the project, call the WebService interface commonly used to two methods, I think it is easier to use, recorded here.

The first, with the httpclient request, which is the same as the HTTP request we often use, the result can be a string that returns XML format, and it's easier for us to parse it and get the data we want.

Address: Columns such as HTTP://WWW.XXX.COM/WEATHERWS/WEATHER.ASMX?WSDL

The method called is: Getcityforecastbyzip method parameter is: ZIP

The required jars are as follows:

Commons-codec-1.4.jar

Commons-httpclient-3.1.jar

Jsoup-1.6.2.jar

Commons-logging-1.0.4.jar

Code:

InputStream is = null;

HttpClient client = new HttpClient ();

Postmethod method = new Postmethod ("Www.xxx.com/WeatherWSS/Weather.asmx/GetCityForecastByZIP");

Method.setrequestheader ("Host", "www.xxx.com");

Method.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

Method.setparameter ("ZIP", "90001");

try {

Client.executemethod (method);

is = Method.getresponsebodyasstream ();

Document document = Jsoup.parse (IS, "GBK", "");

System.err.println (document);

catch (Exception e) {

E.printstacktrace ();

}finally{

Method.releaseconnection ();

try {

if (is!=null) {

Is.close ();

}

catch (IOException e) {

E.printstacktrace ();

}

}

My side. The document returns the form of an XML format that can be processed to get the data we want.

As shown in figure:

Second: Using Axis2 to invoke. This axis2 is not universal, and some Web services use Axis2.

Address: Columns such as HTTP://WWW.XXX.COM/WEATHERWS/WEATHER.ASMX?WSDL

The method called is: Getcityforecastbyzip method parameter is: ZIP

The required jars are as follows:

Axis-ant.jar

Axis.jar

Commons-discovery-0.2.jar

Commons-logging-1.0.4.jar

Jaxrpc.jar

Wsdl4j-1.5.1.jar

Code:

try{

Indicates the URL where the service is located

String endpoint = "Http://www.xxx.com/WeatherWS/Weather.asmx";

String targetnamespace = "http://www.xxx.com/WeatherWS/";

String method= "Getcityforecastbyzip";

Create a service invocation (call)

Service service = new service ();

Call call = (call) Service.createcall ()//Create a Call object via service

Set the URL where the service is located

Call.settargetendpointaddress (new Java.net.URL (endpoint));

Call.setoperationname (New QName (targetnamespace, method));

Call.setusesoapaction (TRUE);

Call.addparameter (New QName (targetnamespace, "ZIP"), Org.apache.axis.encoding.XMLType.XSD_STRING, Javax.xml.rpc.ParameterMode.IN);//Interface parameters

Call.setreturnclass (Org.w3c.dom.Element.class);

Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_STRING);//Set return type

String Path = targetnamespace + method;

Call.setsoapactionuri (path);

Object ret = Call.invoke (new object[] {"90001"});

System.out.println ("xx===" +ret.tostring ());

}catch (Exception e) {

E.printstacktrace ();

}



I use httpclient requests more often. You can also use Wsdl2java to convert a WSDL file to a local service class, which can then be invoked directly.


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.