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;
}
例:
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;
}
相关文章推荐
- 使用URL工具类调用webservice接口(soap)与http接口的实现方式
- 为什么很多第三方接口,都改成了基于http,直接传递json数据的方式来代替webservice?
- 为什么很多第三方接口,都改成了基于http,直接传递json数据的方式来代替webservice?
- Android 直接写和读XML串方式 调用 WebService soap
- get/post方式调用http接口
- 不同方式调用webservice接口
- VB调用WebService(直接Post方式)并解析返回的XML
- JAVA 调用HTTP接口POST或GET实现方式
- get/post方式调用http接口
- 使用 HttpWebRequest的psot方式传参数调用接口方法
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- Java 以post请求方式通过json格式调用Webservice接口
- HTTP get方式调用接口
- webservice调用方式http和soap的区别
- Java 调用C# webservice接口 生成java客户端 实现方式
- get/post方式调用http接口
- C#通过http post方式调用需要证书的webservice
- java调用webservice WSDL形式接口之apache axis方式调用(一)