package com.wistron.wh.swpc.portal.uitl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.wistron.swpc.framework.exception.SystemException;

/**
* 調用webservice的工具,封裝了post,get,put,delete方法調用webservice
*
*/
public class WebServiceUtil {

// 日志记录器
private static Logger logger = Logger.getLogger(WebServiceUtil.class);

/*
* public static String postByMap(String url, Map<String, Object> pv) throws
* SystemException{ logger.debug(String.format("Request url:%s", url));
* String responseMsg = ""; PostMethod postMethod = null; try { HttpClient
* httpClient = new HttpClient();
* httpClient.getParams().setContentCharset("utf-8"); postMethod = new
* PostMethod(url);
*
* Set<String> set = pv.keySet(); Iterator<String> it = set.iterator();
* while (it.hasNext()) { String key = it.next(); Object value =
* pv.get(key); if(null != value) postMethod.addParameter(key,
* value.toString()); else postMethod.addParameter(key, ""); }
*
* httpClient.executeMethod(postMethod);// 200 responseMsg =
* postMethod.getResponseBodyAsString().trim(); } catch(Exception e){ throw
* new SystemException("webservice请求异常",e ); } finally {
* postMethod.releaseConnection(); }
*
* return responseMsg; }
*/

/**
* post调用
*
* @param url
* @param xml
* @return
* @throws SystemException
*/
public static String postMethodInvoke(String url, String xml)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String responseMsg = null;

try {
HttpEntity re = new StringEntity(xml, "utf-8");
httppost.setHeader("Content-Type", "application/xml;charset=utf-8");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
responseMsg = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseMsg;

}

/**
* get调用
*
* @param url
* @return
* @throws Exception
*/
public static String getMethodInvoke(String url) throws Exception {
logger.debug(String.format("Request url:%s", url));
String responseMsg = "";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
httpget.setHeader("Content-Type", "application/xml; charset=utf-8");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
responseMsg = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseMsg;
}

/**
* put
*
* @param url
* @param xml
* @return
* @throws SystemException
*/
public static String putMethodInvoke(String url, String xml)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);
String state = null;
try {
HttpEntity re = new StringEntity(xml);
httpput.setHeader("Content-Type", "charset=utf-8");
httpput.setEntity(re);
HttpResponse response = httpClient.execute(httpput);
state = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return state;

}

/**
* delete
*
* @param url
* @return
* @throws Exception
*/
public static String deleteMethodInvoke(String url) throws Exception {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpDelete httpdelete = new HttpDelete(url);
String state = null;
try {
httpdelete.setHeader("Content-Type", "charset=utf-8");
HttpResponse response = httpClient.execute(httpdelete);
state = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpClient.getConnectionManager().shutdown();
}
return state;

}

/**
* post 文件下载
*
* @param url
* @param file
* @return
* @throws SystemException
*/
public static String postUploadMethodInvoke(String url, File file)
throws SystemException {
logger.debug(String.format("Request url:%s", url));
DefaultHttpClient httpClient = new DefaultHttpClient();

String state = null;
FileInputStream fis = null;
BufferedInputStream in = null;
try {
HttpPost httppost = new HttpPost(url);
fis = new FileInputStream(file);
in = new BufferedInputStream(fis);

HttpEntity re = new InputStreamEntity(in, file.length(),
ContentType.MULTIPART_FORM_DATA);
httppost.setHeader("Content-Type", "charset=utf-8");
httppost.setHeader("enctype", "multipart/form-data'");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost);
HttpEntity e = response.getEntity();
state = EntityUtils.toString(e == null ? new StringEntity("") : e);
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
try {
if (in != null) {
in.close();
}
if (fis != null) {
fis.close();
}
httpClient.getConnectionManager().shutdown();
} catch (IOException e) {
throw new SystemException("webservice请求异常", e);
}
}
return state;
}

}

cxf Webservice 使用httpClient 调用的更多相关文章

  1. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  2. java Webservice(一)HttpClient远程调用

    我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容      2.通 ...

  3. httpclient调用webservice接口的方法实例

    这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码 http://testhcm.y ...

  4. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  5. ASP.NET Core教程:在ASP.NET Core中使用HttPClient调用WebService

    一.前言 在以前的一篇文章中,曾经讲述过如何在ASP.NET Core中调用WebService.但是那种方式是通过静态引用的方式去调用的,如果是在生产环境中,肯定不能使用这种方式去调用,幸运的是微软 ...

  6. CXF WebService 教程

    业务需求:常见WEB服务: 手机淘宝.京东…. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的“进销存系统”在某商品缺货时自动给供应商下订单 ..... ...

  7. Spring整合CXF webservice restful 实例

    webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了. 用到的基础类 User.java @Xm ...

  8. CXF 动态创建客户端调用稳定版本号为2.7.18

    今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging. ...

  9. CXF WebService整合SpringMVC的maven项目

    首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/de ...

随机推荐

  1. DButils实现增删查改

    获取数据库连接 static Connection con=JdbcUtils.MyUtils();//这个连接类静态获取要自己定义 插入 public static void insert() th ...

  2. oa tomcat 代码处理跨域问题

    meta标签处理http.https跨域 <!-- 将http请求转成https请求 --> <meta http-equiv="Content-Security-Poli ...

  3. Oracle 把查询的多个字段赋值给多个变量

    select f1,f2,f3 into v1,v2,v3 from tab1

  4. C#中用OLEDB操作EXCEL时,单元格内容长度超过255被截断

    C#中Microsoft.ACE.OLEDB.12.0 驱动读取excel,会读取前8行来判定每列的数据类型,假如没有超过255个字符,那么会被设置为nvarchar(255),从第9行开始,超过25 ...

  5. WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)

    原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...

  6. WPF模拟探照灯文字

    原文:WPF模拟探照灯文字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1835936 ...

  7. visual studio Web发布至 IIS WebDeploy出错(未能创建SSL/TLS安全通道)Could not create SSL/TLS secure channel

    问题发生的原因是VS 15.9尝试使用系统默认值进行TLS握手,但是要在VS内的某处设置为TLS1.2. 此问题的解决方法是在部署项目的IIS服务器上启用TLS 1.2.例如,请按照此文章中的说明操作

  8. 如何构造树状 JSON 数据 JSON-Tree

    十年河东,十年河西,莫欺骚年穷...打错一个字哈.~_~ 接着上一篇博客,上一篇博客是=使用数据库结合LINQ构造的,为了方便理解,本篇采用泛型分组进行构造. 有兴趣的小虎斑可以参考上一篇博客:如何构 ...

  9. 【APIO2016】烟火表演

    题面 题解 神仙题目啊QwQ 设\(f_i(x)\)表示以第\(i\)个点为根的子树需要\(x\)秒引爆的代价. 我们发现,这个函数是一个下凸的一次分段函数. 考虑这个函数合并到父亲节点时会发生怎样的 ...

  10. RDMA技术解析

    文章出处:https://mp.weixin.qq.com/s/pW-tQR4AYr1Gtd4dpHVW7w 摘要:远程直接内存访问(即Remote Direct Memory Access)是一种直 ...