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. 【Codeforces 1110D】Jongmah

    Codeforces 1110 D 题意:给\(n\)个麻将,每个麻将上有一个\(1..m\)的整数\(a_i\). 现在要将这些麻将们分成一个一个三元组,有两种情况: \([i-1,i,i+1]\) ...

  2. spring-cloud-starter-feign 等jar无法reimport的解决方案

    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...

  3. 【小程序】当遇到bindTap绑定无法跳转到tabbar页面时

    如下图: 更换成navigator包裹跳转也不起作用. cart目录在app.json中定义在底部tabBar中 在小程序 导航 文档 最下方表示 所以,以上应改为

  4. Luogu4423 BJWC2011 最小三角形 平面最近点对

    传送门 题意:给出$N$个点,求其中周长最小的三角形(共线的也计算在内).$N \leq 2 \times 10^5$ 这道题唤起了我对平面最近点对的依稀记忆 考虑平面最近点对的分治,将分界线两边的求 ...

  5. AtCoder ExaWizards 2019 简要题解

    AtCoder ExaWizards 2019 简要题解 Tags:题解 link:https://atcoder.jp/contests/exawizards2019 很水的一场ARC啊,随随便便就 ...

  6. item 7:当创建对象的时候,区分()和{}的使用

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 从不同的角度来看,在C++11中,对象初始化拥有多种语法选择,这体 ...

  7. getUserMedia API及HTML5 调用摄像头和麦克风

    getUserMedia API简介 HTML5的getUserMedia API为用户提供访问硬件设备媒体(摄像头.视频.音频.地理位置等)的接口,基于该接口,开发者可以在不依赖任何浏览器插件的条件 ...

  8. B. Forgery

    链接 [http://codeforces.com/contest/1059/problem/B] 题意 要伪造医生签名,先给你医生的签名nm的网格'.'表示空白',#'表示墨水,你的笔可以这么画以一 ...

  9. 【ML】Two-Stream Convolutional Networks for Action Recognition in Videos

    Two-Stream Convolutional Networks for Action Recognition in Videos & Towards Good Practices for ...

  10. 个人博客作业Week2(9月30日)

    一.是否需要有代码规范 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 这些规范并不是一开始就有的,也不是由某个人规定的,代码规范是程序员们在不断地编程实践过 ...