webService interface call

Personally, I will summarize several common interface calling methods, depending on what kind of webService interface the other party provides. If there is any error, please correct me;

1. When I used to play WeChat public account development, I called the interface of Baidu Translate, which is the interface of this form:

/**
* Translation (Chinese->English-English->Chinese-Japanese->Chinese)

* @param source
* @return
*/
public static String translate(String source) {
String dst = null;


// Assemble the query address
String requestUrl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=CaHtZCTje6XaaYp1tSZN4RAa&q={keyWord}&from=auto&to=auto";
// urlEncode utf-8 encoding the value of parameter q
requestUrl = requestUrl.replace("{keyWord}", urlEncodeUTF8(source));


// query and parse the result
try {
// Query and get the returned result
String json = httpRequest(requestUrl);
// Convert json to TranslateResult object through Gson tool
TranslateResult translateResult = new Gson().fromJson(json,
TranslateResult.class);
// Get the translation in translateResult
dst = translateResult.getTrans_result().get(0).getDst();
} catch (Exception e) {
e.printStackTrace ();
}


if (null == dst)
dst = "Translation system exception, please try later!";
return dst;
}

/**
* Initiate an http request to get the returned result

* @param requestUrl
* request address
* @return
*/
public static String httpRequest(String requestUrl) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url
.openConnection();


httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);


httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();


// Convert the returned input stream to a string
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);


String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// release resources
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();


} catch (Exception e) {
}
return buffer.toString();
}


2. CXF interface call;


public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ICXFService.class);
factory.setAddress("http://localhost:8081/ws/cxf/services/helloworld?wsdl");
ICXFService client = (ICXFService) factory.create();
System.out.println(client.sayHello("andrewLau")); //sayHello provides methods for the interface;
}


3. AXIS interface call


public static void main(String[] args) {
try {
String url = "http://localhost:8081/axis/services/HelloHandler?wsdl";
Service serv = new Service();
Call call = (Call) serv.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(url, "hello"));
String result = (String) call.invoke(new Object[] { "xiexx" }); //invoke provides methods for the interface
System.out.println(result);
} catch (ServiceException e) {
e.printStackTrace ();
} catch (RemoteException e) {
e.printStackTrace ();
}
}


4. The XFIRE interface calls R

public static void main(String[] args) throws Exception {
String url = "http://localhost:8081/ws/services/HelloRmi";
Service serv = new ObjectServiceFactory().create(IHelloRmi.class);
IHelloRmi service = (IHelloRmi) new XFireProxyFactory().create(serv,
url);
String result = service.hello("xiexx"); //hello provides methods for the interface
System.out.println(result);
}


5. Another way is to use the webservice client of eclipse to automatically generate it directly with the wsdl address, without needing to care what the interface is, and call it directly

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775068&siteId=291194637