java 调用web服务的方法

来源:互联网 发布:unity3d如何导入模型 编辑:程序博客网 时间:2024/05/22 00:30

1、使用HttpClient

需求用到JAR文件:commons-httpclient-3.1.jar

直接上代码

/** * 同步工作 *  * @param 接口码 * @param strXml * @param 附件名称数组 * @param 附件内容数组 *            ,使用base64String * @return 字符串 */public String SyncWorkItemLog(String apiCode, String strXml,String[] fileNames, String[] fileContent) {String ret = null;strXml = Util.htmEncode(strXml);//实例SttringBuffer使用于组合Soap请求数据包StringBuffer sb = new StringBuffer();sb.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");sb.append("<SOAP-ENV:Body>");sb.append("<m:SyncWorkItemLog xmlns:m=\"http://ckpower.net/\">");sb.append("<m:apiCode>" + apiCode + "</m:apiCode>");sb.append("<m:strXmlInfo>" + strXml + "</m:strXmlInfo>");sb.append("<m:attachNameList>");if (fileNames != null && fileNames.length > 0) {for (int i = 0; i < fileNames.length; i++) {sb.append("<m:string>");sb.append(fileNames[i]);sb.append("</m:string>");}}sb.append("</m:attachNameList>");sb.append("<m:listByteArr>");if (fileContent != null && fileContent.length > 0) {for (int i = 0; i < fileContent.length; i++) {sb.append("<m:base64Binary>");sb.append(fileContent[i]);sb.append("</m:base64Binary>");}}sb.append("</m:listByteArr>");sb.append("</m:SyncWorkItemLog></SOAP-ENV:Body></SOAP-ENV:Envelope>");PostMethod postMethod = new PostMethod(url);HttpClient httpClient = new HttpClient();InputStream is = null;try {byte[] b = sb.toString().getBytes("utf-8");is = new ByteArrayInputStream(b, 0, b.length);RequestEntity re = new InputStreamRequestEntity(is, b.length,"application/soap+xml; charset=utf-8");postMethod.setRequestEntity(re);int statusCode = httpClient.executeMethod(postMethod);System.out.println(sb.toString());String soapRequestData = postMethod.getResponseBodyAsString();//soapRequestData就是调用web服务的Soap响应数据,是xml格式的,可以通过解析soapRequestData来获得调用web服务的返回值。return soapRequestData;} catch (Exception ex) {System.out.println("接口调用失败!" + ex.getMessage());} finally {try {if (is != null)is.close();} catch (Exception ex) {System.out.println("is.close()失败!" + ex.getMessage());}}return ret;}


 

2、使用Xfire
 用到的jar文件xfire-all-1.2.4.jar, jdom-1.0.jar

直接上代码

 

public class WsClient {private String url; //webservice URLprivate Client client = null;public void setUrl(String url) {this.url = url;}public WsClient() {}private void init() {if (client == null) {try {client = new Client(new URL(url));} catch (MalformedURLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}/** * @param in0 * @param in1 * @param in2 * @param in3 * @param in4 * @param in5 * @param in6 * @param in7 * @return * @throws Exception */public String StartWorkItem(String in0, String in1, String in2,String in3, String in4, byte[] in5, String in6, String in7)throws Exception {String result = null;this.init();Object[] results = client.invoke("StartWorkItem", new Object[] { in0,in1, in2, in3, in4, in5, in6, in7 });if (results != null) {result = results[0].toString();}return result;}}


 

3、使用axis2
 下载axis2-1.4
 方法:
 打开控制台,进入axis2-1.4/bin目录
wsdl2java.bat -uri http://192.168.3.97:8088/WebServiceAPI.asmx?WSDL -p ws.clinet.axis2


 上述命令执行完后,会在当前目录下生成一个src目录,在src\ ws\ clinet\ axis2目录里生成XXXXCallbackHandler.java和XXXXStub.java两个文件。
 wsdl2java 会根据wsdl文件生成web服务的调用接口,参数类,返回值的类。
 在调用webservice的时候直接实例化一个XXXXStub的对象,然后调用web服务的方法就可以了。

 

4、 总结
上述的调用web服务的方法是通用的。
 上述三种方法中使用httpclient应该是比较灵活,但是开发效率低,难度大,使用Xfire和axis2比较容易,开发速度快,但是axis2通用性不好,有的web服务用axis2不好用。httpclient和Xfire通用性比较好,鉴于以上特点推荐使用Xfire。

另外:现在实际使用中发现在Xfire对一些复杂处理不很好。 使用HttpClient可以解决。Web服务Soap分析工具建议使用XmlSpy + Fiddler2

原创粉丝点击