最近用到这些 整理了一下
http请求 客户端调用http请求代码:
?/**
? * <pre>
? * 调用http服务端
? * 请求的参数:平台代码、签名、请求xml
? * 注:同一接口有不同版本在URL中区分,如 http://localhost:8080/test/recvPoController/v1.0/recv,其中“v1.0”即为请求版本
? * </pre>
? *
? * @param clientConfig
? * @param reqXml
? * @return
? * @throws Exception
? */
?public String invokeHttpServer(String mscpKey,String httpUrl,String mscpCode,String reqXml) throws Exception {
??logger.debug("调用市平台服务接口开始...");
??//调试或者医院联调时可以将此log展开,正式使用请注释掉改行log? 正式数据的数据量大
??logger.debug("调用市平台服务接口请求的xml:",reqXml);
??HttpPost httppost = null;
??CloseableHttpResponse response = null;
??try {
???httppost = new HttpPost(httpUrl);
???// 将请求的xml格式的字符串进行压缩
???String zipReqxml = ZipUtil.zipBase64String(reqXml);
???// 签名的参数传没解压前的 服务器那边的签名就不需要在进行解压处理
???String sign = MD5Util.sign(reqXml,mscpKey,"utf-8");
???logger.debug("调用市平台服务接口,sign: {}",sign);
? // 下面的几个参数? 服务器那边可以用request.getparams拿到 或者直接写到服务端请求的方法参数中
???List<NameValuePair> params = new ArrayList<NameValuePair>();
???params.add(new BasicNameValuePair("mscpCode",mscpCode));
???params.add(new BasicNameValuePair("sign",sign));
???params.add(new BasicNameValuePair("reqXml",zipReqxml));
???httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
???response = HttpClients.createDefault().execute(httppost);
???String result = null;
???logger.debug("调用市平台服务接口后返回的状态码:" + response.getStatusLine().getStatusCode());
???if (response.getStatusLine().getStatusCode() == 200) {
????result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
???}
???// logger.debug("clientConfig[{}] 调用市平台服务接口响应:{}",result);
???return result;
??} catch (Exception e) {
???logger.debug("调用http服务端接口出错",e);
???throw e;
??} finally {
???if (response != null) {
????try {
?????response.close();
????} catch (Exception e2) {
?????logger.debug("关闭连接异常",e2);
????}
???}
???if (httppost != null) {
????httppost.abort(); // 结束后关闭请求
???}
??}
?}
webservice请求 客户端调用请求代码:
private String invokeMscpService(String method,Object... args) {
??logger.debug("method[{}] 调用平台服务接口开始...",method);
??try {
???Service service = new Service(); // 创建调用对象
???Call call = (Call) service.createCall();
???call.setOperationName(new QName(namespaceUri,method)); // 调用服务端的method
???call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
???String ret = (String) call.invoke(args);
???return ret;
??} catch (Exception e) {
???logger.debug("调用平台服务接口出错1",e);
???// return "调用平台服务接口出错";
???throw new RuntimeException(e);
??} finally {
???logger.debug("method[{}] 调用平台服务接口结束",method);
??}
?}
??