WebService接口直接http調用方式

WebService接口除了用XFire和As方式調用,還可以直接用HttpClient 的方式直接調用哦!

例:

 private HashMap<String,String> getData(String serviceCode, String xmlPara){
    	HashMap<String,String> res = new HashMap<>();
    	String endpoint = "http://124.205.248.2:8080/eSales/esales.asmx?WSDL";
		PostMethod postMethod = new PostMethod(endpoint);
		byte[] b;
		try {
			b = xmlPara.getBytes("utf-8");
			InputStream is = new ByteArrayInputStream(b,0,b.length);
			RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
			//把Soap請求數據添加到PostMethod中
			postMethod.setRequestEntity(re);
			
			//生成一個HttpClient對象,併發出postMethod請求
			HttpClient httpClient = new HttpClient();
			int statusCode = httpClient.executeMethod(postMethod);
			if(200==statusCode){
				String getServerData =  postMethod.getResponseBodyAsString();
				//System.out.println("----->"+getServerData);
				//獲取返回值狀態標識,標識爲0:成功;非0:失敗
				res.put("status", "0");
				res.put("msg", msg);
			}
		} catch (Exception e) {
			res.put("status", "1");
			res.put("msg", e.toString());
			e.printStackTrace();
		}
		return res;
    }


發佈了31 篇原創文章 · 獲贊 12 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章