httpClient访问webservice服务--带http头参数
来源:互联网 发布:linux开机启动shell 编辑:程序博客网 时间:2024/05/16 08:39
httpClient访问webservice
之所以通过httpClient访问是因为有的webservice需要设置http header,比如利用soapUI工具时会设置http header,那么利用axis就没有办法设置http头(我没有找到),所以就需要利用httpClient进行webserivce访问,代码如下:
1, getData方法
private static String getData(String OperationCode, String param){ String returnVal = "" ; StringBuilder xmlpara = new StringBuilder() ; xmlpara.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:user=\"http://127.0.0.1:8080/UserAction\">") ; xmlpara.append("<soapenv:Header/>") ; xmlpara.append("<soapenv:Body>") ; xmlpara.append("<user:getUserDetailByName>") ; xmlpara.append("<user:param>" + param + "</user:param>") ; xmlpara.append("</user:getUserDetailByName>") ; xmlpara.append("</soapenv:Body>") ; xmlpara.append("</soapenv:Envelope>") ; String inputPara = xmlpara.toString() ; HashMap<String,String> res = new HashMap<>();// String endpoint = "http://124.205.248.2:8080/eSales/esales.asmx?WSDL"; String endpoint = "http://192.168.0.123:9090/esb.user"; PostMethod postMethod = new PostMethod(endpoint); byte[] b; try { b = inputPara.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); postMethod.setRequestHeader("ClientId", "com.primeton.esb.hljsjzx.manage") ;// postMethod.setRequestHeader("OperationCode", "com.primeton.esb.hljsjzx.Infrastructure.user.getUserDetailByName") ; postMethod.setRequestHeader("OperationCode", OperationCode) ; //生成一个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", getServerData); SAXReader sax = new SAXReader() ; Document doc = sax.read(new ByteArrayInputStream(getServerData.getBytes("utf-8"))) ; DefaultXPath xpath = new DefaultXPath("//ns1:out"); xpath.setNamespaceURIs(Collections.singletonMap("ns1","http://127.0.0.1:8080/UserAction")); Element ele = (Element)xpath.selectSingleNode(doc) ; returnVal = ele.getText(); } } catch (Exception e) { res.put("status", "1"); res.put("msg", e.toString()); e.printStackTrace(); } return returnVal; }
- 方法调用
public static void main(String[] args) { String operationCode = "com.primeton.esb.hljsjzx.Infrastructure.user.getUserDetailByName" ; Map map = getData(operationCode,xmlpara.toString()) ; String val = "" ; String status = (String)map.get("status"); if(status.equals("0")){ val = (String)map.get("msg") ; } SAXReader sax = new SAXReader() ; try { Document doc = sax.read(new ByteArrayInputStream(val.getBytes("utf-8"))) ; DefaultXPath xpath = new DefaultXPath("//ns1:out"); xpath.setNamespaceURIs(Collections.singletonMap("ns1","http://127.0.0.1:8080/UserAction")); Element ele = (Element)xpath.selectSingleNode(doc) ; System.out.println(ele.getText()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
阅读全文
0 0
- httpClient访问webservice服务--带http头参数
- HttpClient访问WebService
- httpclient 访问 webservice
- WCF实现http访问地址后面带参数访问方式
- HttpClient 访问SSL WebService 方式
- Android使用httpclient访问http
- HttpClient中配置HTTP参数
- HttpClient中配置HTTP参数
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 一、Http 请求示例(Get)
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 二、Http POST
- (六) WebService-- HttpClient 调用WS服务
- 为 httpclient 设置代理, 设置http头.
- HttpClient设置HTTP请求头Header
- 为 httpclient 设置代理, 设置http头.
- HttpClient设置HTTP请求头Header
- HttpClient设置HTTP请求头Header
- httpclient访问带cookie限制的网页
- httpclient 面https认证,带cookie访问
- 利用HudText来制作连击爆字
- Matalb中plot绘图命令详解
- 泛型
- xx项目总结
- 2760: [JLOI2011]小A的烦恼
- httpClient访问webservice服务--带http头参数
- AlibabaJava开发手册阅读
- PhpStorm2016.2 缩进设置
- 计算两个数的平均数的几种方法解读
- canny边缘检测
- struts2单例多例问题以及spring整合的问题
- RMQ 求区间最值
- 视频播放
- Visible Trees 【容斥】