文档章节

cxf Webservice 使用httpClient 调用

D
 D_Bug
发布于 2016/08/30 09:52
字数 580
阅读 32
收藏 0

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;
    }

}

© 著作权归作者所有

D
粉丝 0
博文 6
码字总数 6813
作品 0
昌平
私信 提问
CXF WebService Hello World

由于公司现在是.Net系统于Java系统并存,项目中难免需要跨平台的远程服务调用。最近刚好有一个项目需要Java Web系统调用.Net提供的WebService服务。权衡了下各个因素,最后决定使用 Apache C...

奋斗的海贼
2013/01/02
222
0
[activiti]activiti6.x调用RESTful服务例子

pom.xml,cxf等没用可删除(之前webservice用)

Danni3
03/20
116
0
cxf+spring发布webservice和调用

项目所需jar包:http://url.cn/V6RvIw 因为我用到了httpclient,所以里面包含了httpclient的jar包。 建项目什么就不说了。 直接上代码说接口: 写一个demo接口 package cn.cxf.demo; import ...

JohnnyFu
2014/12/21
8.2K
3
[CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1、介绍RESTful架构风格 2、Spring配置CXF 3、三层初设计,实现WebService接口...

泥沙砖瓦浆木匠
2015/02/26
5.4K
1
spring+restful service

Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的。关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述...

游宵
2014/04/25
1K
1

没有更多内容

加载失败,请刷新页面

加载更多

Echars鼠标点击事件多次触发

本文转载于:专业的前端网站➭Echars鼠标点击事件多次触发 gChart.on('click', function (params) { if (params.componentSubType == "bar" && params.componentType == "serie......

前端老手
23分钟前
4
0
springboot2.0.6(附) 解析META-INF/spring.factories通过系统加载类获取对应的 class 的全限定名称

在 SpringBoot中是通过getSpringFactoriesInstances(Class<T> type)方法获取所有classpath下面的META-INF/spring.factories文件,然后根据type值找到对应的 class 的全限定名称列表。下面我来...

小亮89
24分钟前
4
0
zk工厂方法实现NIOServerCnxnFactory

NIOServerCnxnFactory类 内部类 AbstractSelectThread AcceptThread SelectorThread 属性 ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT 10s session过期时间 ZOOKEEPER_NIO_NUM_SELECTOR_THREADS......

writeademo
28分钟前
4
0
天猫精灵业务如何使用机器学习PAI进行模型推理优化

作者:如切,悟双,楚哲,晓祥,旭林 引言 天猫精灵(TmallGenie)是阿里巴巴人工智能实验室(Alibaba A.I.Labs)于2017年7月5日发布的AI智能语音终端设备。天猫精灵目前是全球销量第三、中国...

阿里云官方博客
35分钟前
6
0
BJL技巧之大路馆擦法—《利辰逸》

很多朋友刚玩百佳时间不长,不知道如何看路,今 天我就来简单的讲下看大路的方法。 都是前辈教的,这里我只是简单复述一下。 大路的观察方法有以下几种: 先从庄闲的数量变化说起。 庄闲的强...

那女孩对我说
36分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部