java 調用web服務的方法

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 URL
	private 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章