cxf Webservice 使用httpClient 调用

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

}

转载于:https://my.oschina.net/java12/blog/739638

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值