WebService接口直接http调用方式

时间: 2023-08-15 admin 互联网

WebService接口直接http调用方式

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;
    }