Java之~ HTTPS协议doPost,doPut,doDelete,doGet调用工具类

一.基本上这样,如有不同大体修改一下就可以

写一个这样的HttpClientUtils 工具类

public class HttpClientUtils {

    /**
     * post请求
     * @param url
     * @param authorValue
     * @param json
     * @return
     */
    public static JSONObject doPost(String url, String authorValue, JSONObject json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        if(StringUtils.isNotEmpty(authorValue)){
            post.setHeader("Authorization",authorValue);
        }
        JSONObject response = null;
        try {
            response = getJsonObject(json, client, post);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
    public static JSONObject doPost(String url, String authorValue, JSONArray json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        if(StringUtils.isNotEmpty(authorValue)){
            post.setHeader("Authorization",authorValue);
        }
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);

            HttpResponse res = client.execute(post);
            HttpEntity entity = res.getEntity();
            String result = EntityUtils.toString(entity);// 返回json格式:
            response = JSONObject.fromObject(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

    private static JSONObject getJsonObject(JSONObject json, DefaultHttpClient client, HttpEntityEnclosingRequestBase base) throws IOException {
        JSONObject response;StringEntity s = new StringEntity(json.toString());
        s.setContentEncoding("UTF-8");
        s.setContentType("application/json");//发送json数据需要设置contentType
        base.setEntity(s);

        HttpResponse res = client.execute(base);
        HttpEntity entity = res.getEntity();
        String result = EntityUtils.toString(entity);// 返回json格式:
        response = JSONObject.fromObject(result);
        return response;
    }

    /**
     * put请求
     * @param url
     * @param authorValue
     * @param json
     * @return
     */
    public static JSONObject doPut(String url, String authorValue, JSONObject json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPut put = new HttpPut(url);
        if(StringUtils.isNotEmpty(authorValue)){
            put.setHeader("Authorization",authorValue);
        }
        JSONObject response = null;
        try {
            response = getJsonObject(json, client, put);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
    /**
     * delete请求
     * @param url
     * @param authorValue
     * @param dataForm
     * @return
     */
    public static JSONObject doDelete(String url, String authorValue,Map<String,Object> dataForm){
        HttpClient httpClient = new HttpClient();
        DeleteMethod deleteMethod = new DeleteMethod(url);
        if(StringUtils.isNotEmpty(authorValue)){
            deleteMethod.setRequestHeader("Authorization",authorValue);
        }
        List<NameValuePair> data = new ArrayList<NameValuePair>();
        if(dataForm!=null){
            Set<String> keys = dataForm.keySet();
            for(String key:keys){
                NameValuePair nameValuePair = new NameValuePair(key, (String) dataForm.get(key));
                data.add(nameValuePair);
            }
        }
        deleteMethod.setQueryString(data.toArray(new NameValuePair[0]));
        try {
            int statusCode = httpClient.executeMethod(deleteMethod);
            // Read the response body.
            byte[] responseBody = deleteMethod.getResponseBody();
            return JSONObject.fromObject(new String(responseBody));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            deleteMethod.releaseConnection();
        }
        return null;

    }
    /**
     * get 请求
     * @param url
     * @param authorValue
     * @param para
     * @return
     */
    public static JSONObject doGet(String url,String authorValue,Map<String, String> para){
        JSONObject response = null;
        try{
            DefaultHttpClient client = new DefaultHttpClient();

            URIBuilder builder = new URIBuilder(url);
            Set<String> set = para.keySet();
            for(String key: set){
                builder.setParameter(key, para.get(key));
            }
            HttpGet request = new HttpGet(builder.build());
            if(StringUtils.isNotEmpty(authorValue)){
                request.setHeader("Authorization",authorValue);
            }
            request.setHeader("ContentTye","application/json");
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(6000)
                    .setConnectTimeout(6000)
                    .setConnectionRequestTimeout(6000).build();
            request.setConfig(requestConfig);


            HttpResponse res = client.execute(request);
//            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                response = JSONObject.fromObject(result);
//            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值