Use AXIS2 for Client access WebService

Source: Internet
Author: User

Using AXIS2, you can easily build WebService server side, also can be very convenient as cilent, to access other webservice.

Below according to the experience of the work, collated a bit, as cilent visit WebService points.


According to Axis2 's official documentation, there are 3 ways to databinding the client, the simplest of which seems to be the ADB, then I choose the adb.


1. Normal mode (HTTP does not pass proxy, the other party does not use SSL)

Generate clientrpcserviceclient serviceclient = new Rpcserviceclient (); Options options = Serviceclient.getoptions () ;//Generate endpointstring WebServiceURL = "Http://www.abc.net/webservice/servicepage"; For example. EndpointReference Targetepr = new EndpointReference (WebServiceURL); Options.setto (TARGETEPR);//Auto release Transport.options.setCallTransportCleanup (TRUE);//Generate actionstring ns = "Http://www.abc.net/webservice"; String action = "getsomething"; QName opaction = new QName (NS, action);//Generate reqest Parametersreqbean reqobj = new Reqbean (); Reqobj.setparam1 ("Param 1 "); Reqobj.setparam2 (" param2 "); object[] Opargs = new object[] {reqobj}; class[] Returntypes = new class[] {arraylist.class};object[] response = null;try {    response = Serviceclient.invokebl Ocking (Opaction, Opargs, returntypes);} catch (Axisfault af) {    //Process exception.} ArrayList res = (ArrayList) response[0];//Analyze the response.//...

Reqbean is a Java-based Bean class that can be generated from WSDL based on the parameter required by the webservice being accessed.

The returned response, unified first converted to ArrayList, and then further parsed into strings or individual beans.


This is the easiest way to access it.


2. Access via SSL. (HTTPS)

Most of the time, the client does not get the server's certificate in advance to cause an error. So I need to update the Protocol of communication.

The official website is not very detailed, so list the code.

2.1 Generate a new Protocol factory class:

Import Java.io.ioexception;import java.net.inetaddress;import java.net.inetsocketaddress;import java.net.Socket; Import Java.net.socketaddress;import Java.net.unknownhostexception;import java.security.cert.CertificateException ; Import Java.security.cert.x509certificate;import Javax.net.socketfactory;import Javax.net.ssl.sslcontext;import Javax.net.ssl.trustmanager;import Javax.net.ssl.x509trustmanager;import Org.apache.commons.httpclient.connecttimeoutexception;import Org.apache.commons.httpclient.HttpClientError; Import Org.apache.commons.httpclient.params.httpconnectionparams;import Org.apache.commons.httpclient.protocol.protocolsocketfactory;public class Sslignoreerrorprotocolsocketfactory implementsprotocolsocketfactory {private Sslcontext sslcontext = null;/** * No certificate verification * * @return */private static Sslcon Text Createeasysslcontext () {try {sslcontext context = sslcontext.getinstance ("SSL"); Context.init (NULL, new Trustmanager[] {new X509trustmanager () {public void checkclienttrusted(x509certificate[] arg0,string arg1) throws certificateexception {}public void checkservertrusted (x509certificate[] Arg0,string arg1) throws Certificateexception {}public x509certificate[] getacceptedissuers () {return null;}} }, null); return context;} catch (Exception e) {throw new Httpclienterror (e.tostring ());}} Private Sslcontext Getsslcontext () {if (This.sslcontext = = null) {This.sslcontext = Createeasysslcontext ();} return this.sslcontext;} @Overridepublic Socket Createsocket (String host, int port) throws Ioexception,unknownhostexception {return Getsslcontext (). Getsocketfactory (). Createsocket (host, port);} @Overridepublic Socket Createsocket (String host, int port, inetaddress clienthost,int clientport) throws IOException, Unk nownhostexception {return Getsslcontext (). Getsocketfactory (). Createsocket (Host, Port,clienthost, ClientPort);} @Overridepublic Socket Createsocket (String host, int port, inetaddress localaddress,int LocalPort, Httpconnectionparams params) throws Ioexception,unknownHostexception, Connecttimeoutexception {if (params = = null) {throw new IllegalArgumentException ("Parameters May is not nul L ");} int timeout = params.getconnectiontimeout (); Socketfactory socketfactory = Getsslcontext (). Getsocketfactory (); if (timeout = = 0) {return Socketfactory.createsocket ( Host, port, Localaddress,localport);} else {Socket socket = Socketfactory.createsocket (); SocketAddress localaddr = new inetsocketaddress (localaddress,localport); SocketAddress remoteaddr = new Inetsocketaddress (host, port); Socket.bind (LOCALADDR); Socket.connect (REMOTEADDR, timeout); return socket;}}}

2.2 Use the above class to make a new protocol object. (Sslport is specified according to server-side settings, typically 443.)

Protocol Protocol = null; Sslignoreerrorprotocolsocketfactory socketfactory = Null;socketfactory = new Sslignoreerrorprotocolsocketfactory (); protocol = new Protocol ("https", Socketfactory, Sslport);

2.3 Set the Protocol object made above to the options of Rpcserviceclient.

Options.setproperty (Httpconstants.custom_protocol_handler, PROTOCOL);

Then, you can access the webservice over HTTPS.

Note: Httpconstants is the Org.apache.axis2.transport.http.HTTPConstants class used.


3. Need to pass the proxy server case.

This will be on the official website.

3.1 Build agent Properties:

Httptransportproperties.proxyproperties proxyproperties = new Httptransportproperties.proxyproperties (); Proxyproperties.setproxyname (proxy_server);p roxyproperties.setproxyport ((int) (proxy_port);

Httptransportproperties is the use of org.apache.axis2.transport.http.HttpTransportProperties


3.2 Set the proxy attribute to the rpcserviceclient options.

Options.setproperty (Httpconstants.proxy, proxyproperties);

can be accessed through a proxy server.


In actual work, I use squid test, when found communication, axis2 default use chunked attribute, resulting in cannot pass squid, specific reasons unknown.

Then, when using squid, the chunked attribute disable off.

Options.setproperty (httpconstants.chunked, false);

This point, the official website does not mention, may also be where I set not enough.


Above, is through AXIS2 as a client access to the webservice of several situations, basically the general HTTP environment should be able to deal with almost.

Of course, there are other ways of communication that are not used at work for the time being.


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.