Java Invocation WebService and interface method

Source: Internet
Author: User
WebService of the call, commonly used about there 3 Kinds of ways:
1 , using Axis Call
2 , using Xfire Call
3 , using CXF Call
project, using axis to make the call, as recorded below, Memo:
PS Tutorial : Think of the time is used Xfire Way to call, the results have not been recorded, and now completely remember how to play. So I'm going to write more blogs, t_t .
Version Description:
Aixs version: Axis-bin-1_4.zip
Java Environment slightly
the first step: Make sure that the WSDL file is available, in the text to get to the sendsmsservice.wsdl, of course, the URL of the line.
Step Two: Execute the script that generates the client code. The script reads:
Set Axis_lib=d:axis-1_4lib
Set java_cmd=java-djava.ext.dirs=%axis_lib%
Set output_path=.
Set package= Info.jyzh . Wap.liaoning.push
%java_cmd% Org.apache.axis.wsdl.wsdl2java sendsmsservice.wsdl-o%output_path%-p%package%-T
#查看 Wsdl2java the use of Help # %java_cmd% org.apache.axis.wsdl.wsdl2java-help
OK , so the client code is generated. I brought a unit test.
in actual work, the client cannot connect directly to the following situations WebService server, the middle is blocked by a powerful proxy server. The following figure:
To do This, modify the generated code, which is modified in Sendmmsserviceimplservicesoapbindingstub, as follows:
static {
axisproperties.setproperty ("Http.proxyhost", " the") . the . the . the ");
Axisproperties.setproperty ("Http.proxyport", "8080");
axisproperties.setproperty ("Http.proxyuser", " asp Tutorial yy ");
axisproperties.setproperty ("Http.proxypassword", " 123456 ");
_operations = new Org.apache.axis.description.operationdesc[1];
_INITOPERATIONDESC1 ();

}

Direct axis calls Remote Web service

I think this method is more suitable for the master, they can directly read the XML format WSDL file, I do not understand, especially I do not specialize in this line, even if a period of time to understand, and then forget. The direct call mode is as follows:

Import Java.util.date;

Import Java.text.dateformat;

Import Org.apache.axis.client.call;

Import Org.apache.axis.client.service;

Import Javax.xml.namespace.qname;

Import Java.lang.integer;

Import Javax.xml.rpc.parametermode;

public class Caclient {

public static void Main (string[] args) {

try {

String endpoint = "HTTP://LOCALHOST:8080/CA3/SERVICES/CASYNROCHNIZED?WSDL";

Direct reference to remote WSDL file

Here are the routines.
Service service = new service ();

Call call = (call) Service.createcall ();

Call.settargetendpointaddress (endpoint);

Call.setoperationname ("AddUser"); Interface name described inside//WSDL

Call.addparameter ("username", Org.apache.axis.encoding.xmltype.xsd_date,

javax.xml.rpc.parametermode.in);//Interface parameters

Call.setreturntype (org.apache.axis.encoding.xmltype.xsd_string);//Set return type


The string temp = "Tester";

String result = (string) call.invoke (new object[]{temp});

Pass parameters to the method and call the method

System.out.println ("result is" +result);

}

catch (Exception e) {

System.err.println (E.tostring ());

}

}

}

2, direct SOAP calls remote WebService

I've never seen or tried this pattern, but someone posted it on the internet, and I turned around.

Import org.apache.soap.util.xml.*;

Import org.apache.soap.*;

Import org.apache.soap.rpc.*;

Import java.io.*;

Import java.net.*;

Import Java.util.vector;

public class caservice{

public static string GetService (string user) {

URL url = null;

try {

Url=new URL ("http://192.168.0.100:8080/ca3/services/casynrochnized");

catch (Malformedurlexception Mue) {

return Mue.getmessage ();

}

This is the main soap object

Call Soapcall = new Call ();

Use SOAP encoding

Soapcall.setencodingstyleuri (CONSTANTS.NS_URI_SOAP_ENC);

This is the remote object we ' re asking for the

Soapcall.settargetobjecturi ("urn:xmethods-casynrochnized");

This is the "method" on the above object

Soapcall.setmethodname ("GetUser");

We need to send the ISBN number as a input parameter to the method

Vector soapparams = new vector ();

Name, type, value, encoding style

Parameter Isbnparam = new parameter ("username", string.class, user, NULL);

Soapparams.addelement (Isbnparam);

Soapcall.setparams (Soapparams);

try {

Invoke the remote method on the object

Response Soapresponse = Soapcall.invoke (URL, "");

Check to the If there is a error, return "N/a"

if (Soapresponse.generatedfault ()) {

Fault fault = Soapresponse.getfault ();

string f = fault.getfaultstring ();

return F;

} else {

Read result

Parameter Soapresult = Soapresponse.getreturnvalue ();

Get a string from the

Return Soapresult.getvalue (). ToString ();

}

catch (SoapException se) {

return Se.getmessage ();

}

}

}

3, use Wsdl2java to turn the WSDL file into a local class, and then use it like a local class.

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.