您的位置:首页 > 理论基础 > 计算机网络

WebService接口直接http调用方式

2016-12-12 14:51 423 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息