栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Http接口调用方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Http接口调用方法

调用对方系统的http接口,post请求。

RequestParamsVo requestParamsVo =new RequestParamsVo();
groupRequestParamsVo(requestParamsVo, token, String.valueOf(maxKuduId));

//将requestParamsVo 转为json字符串
String jsonstr = JSONArray.toJSON(requestParamsVo).toString();

//主要是用到了BigdataHttpUtil这个工具类

byte[] orderBuf = BigdataHttpUtil.doPostJson(dataUrl,jsonstr);

ps:工具类 BigdataHttpUtil.java(HttpUtil)

package com.sdp.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import com.global.exception.baseException;
import com.global.util.StringUtil;

public class BigdataHttpUtil {

   
    public static byte[] doPost(String url) {
        Map map = new HashMap();
        return doPost(url, map);
    }

   
    public static byte[] doPost(String url, Map map) {
        return doPost(url, map, "UTF-8");
    }

   
    public static byte[] doPost(String url, Map map, String enCode) {

        HttpClient client = new HttpClient();//22 
        client.getParams().setSoTimeout(30000);
        PostMethod method = new PostMethod(url);
        // 设置字符编码集
        method.getParams().setContentCharset(enCode);
        for (String key : map.keySet()) {
            String value = map.get(key);
            if (StringUtil.isBlank(value))
                continue;
            method.addParameter(key, value);
        }

        try {
            int status = client.executeMethod(method);
            if (status != HttpStatus.SC_OK)
                throw new Exception(String.format("服务器返回错误:[%d]", status));

            return method.getResponseBody();
        } catch (Exception ex) {
            throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
        } finally {
            method.releaseConnection();
        }

    }

    public static byte[] doPost(String url, byte[] buf) {
        return doPost(url, buf, "UTF-8");
    }

    public static byte[] doPost(String url, byte[] buf, String enCode) {
        HttpClient client = new HttpClient();
        client.getParams().setSoTimeout(60000);
        PostMethod method = new PostMethod(url);

        method.setRequestHeader("Content-Type", "application/xml; charset=" + enCode);
        method.setRequestEntity(new ByteArrayRequestEntity(buf));

        try {
            int status = client.executeMethod(method);
            if (status != HttpStatus.SC_OK)
                throw new Exception(String.format("服务器返回错误:[%d]", status));

            return method.getResponseBody();
        } catch (Exception ex) {
            throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
        } finally {
            method.releaseConnection();
        }
    }
    
    public static byte[] doPostJson(String url, String jsonstr) {
        return doPostJson(url, jsonstr, "");
    }
    
    public static byte[] doPostJson(String url, String jsonstr, String sign) {
        HttpClient client = new HttpClient();
        client.getParams().setSoTimeout(60000);
        PostMethod method = new PostMethod(url);

        method.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
        if (StringUtil.isNotBlank(sign)) {
            method.setRequestHeader("sign", sign); // 签名
        }
        
        RequestEntity se;
        try {
            se = new StringRequestEntity (jsonstr ,"application/json" ,"UTF-8");
            method.setRequestEntity(se);
        } catch (UnsupportedEncodingException e) {
            throw new baseException(String.format("发送数据错误:[%s][%s]", e.getMessage(), url), e);
        }
        try {
            int status = client.executeMethod(method);
            if (status != HttpStatus.SC_OK)
                throw new Exception(String.format("服务器返回错误:[%d]", status));
            return method.getResponseBody();
        } catch (Exception ex) {
            throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
        } finally {
            method.releaseConnection();
        }
    }

    public static byte[] doGet(String url) {
        return doGet(url, "UTF-8");
    }

    public static byte[] doGet(String url, String enCode) {
        Map map = new HashMap();
        return doGet(url, map, enCode);
    }

    public static byte[] doGet(String url, Map map, String enCode) {
        HttpClient client = new HttpClient();
        client.getParams().setSoTimeout(60000);
        String Params = "";
        String flag = "";
        if (map != null && !map.isEmpty()) {
            flag = "&";
            if (url.indexOf('?') < 0) {
                flag = "?";
            }
            for (String key : map.keySet()) {
                String value = map.get(key);
                if (StringUtil.isBlank(value))
                    continue;
                if (StringUtil.isBlank(Params)) {
                    Params = flag+key + "=" + value;
                } else {
                    Params += "&"+key + "=" + value;
                }
            }
        }
        GetMethod method = new GetMethod(url + Params);

        // 设置字符编码集
        method.getParams().setContentCharset(enCode);
        try {
            int status = client.executeMethod(method);
            if (status != HttpStatus.SC_OK)
                throw new Exception(String.format("服务器返回错误:[%d]", status));

            return method.getResponseBody();
        } catch (Exception ex) {
            throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
        } finally {
            method.releaseConnection();
        }
    }
    
   
    public static String getParams(String method,Map paramValues)
    {
        String params="";
        Set key = paramValues.keySet();
        String beginLetter="";
        if (method.equalsIgnoreCase("get"))
        {
            beginLetter="?";
        }
 
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            if (params.equals(""))
            {
                params += beginLetter + s + "=" + paramValues.get(s);
            }
            else
            {
                params += "&" + s + "=" + paramValues.get(s);
            }
        }
        return params;
    }
    
   
    public static String getParams(String method,Map paramValues, String encode) throws UnsupportedEncodingException
    {
        String params="";
        Set key = paramValues.keySet();
        String beginLetter="";
        if (method.equalsIgnoreCase("get"))
        {
            beginLetter="?";
        }
 
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            String value = paramValues.get(s);
            if ("1".equals(encode)) {
                value = URLEncoder.encode(paramValues.get(s), "UTF-8");
            }
            if (params.equals(""))
            {
                params += beginLetter + s + "=" + value;
            }
            else
            {
                params += "&" + s + "=" + value;
            }
        }
        return params;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号