HttpClientUtil工具类

本文介绍了一个用于发送HTTP请求的工具类实现,包括GET、POST、PUT和PATCH等请求方式。该工具类使用Apache HttpClient库,能够设置请求头和请求体,并返回服务器响应结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.bootdo.util;

import java.io.IOException;
import java.util.*;

import com.bootdo.device.controller.HttpParamEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

public static String sendUrl(HttpParamEntity httpParamEntity, String type) {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpEntityEnclosingRequestBase httpRquest=null;

    if(type.equals("put")){
        System.out.println("put 操作..............");
        httpRquest = new HttpPut(httpParamEntity.getUrl());
    }else if(type.equals("post")){
        System.out.println("post 操作..............");
        httpRquest = new HttpPost(httpParamEntity.getUrl());
    }else if(type.equals("patch")){
        System.out.println("patch 操作..............");
        httpRquest = new HttpPost(httpParamEntity.getUrl());
    }else if(type.equals("get")){
      //  httpRquest = new HttpGet(httpParamEntity.getUrl());

    }else{

    }


    Map <String,String>headParam = httpParamEntity.getHeadParam();

    Set<Map.Entry<String, String>> set = headParam.entrySet();

    Iterator<Map.Entry<String, String>> iterator = set.iterator();

    Map.Entry<String, String> entry=null;

    while(iterator.hasNext()){
        entry = iterator.next();
        httpRquest.setHeader(entry.getKey(),entry.getValue());
    }


    CloseableHttpResponse httpResponse = null;
    try {
        httpRquest.setEntity(new StringEntity(httpParamEntity.getBody(),"utf-8"));
        httpResponse = httpClient.execute(httpRquest);
        HttpEntity entity = httpResponse.getEntity();
        String result = EntityUtils.toString(entity);
        return result;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (httpResponse != null) {
            try {
                httpResponse.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != httpClient) {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
//调用接口时候的固定调用参数
public static HttpParamEntity getHttp(){
    HttpParamEntity httpParamEntity=new HttpParamEntity();
    httpParamEntity.getHeadParam().put("cpid","167");
    httpParamEntity.getHeadParam().put("key","6f5a3292c3c4731aec91df0a0581c278");
    httpParamEntity.getHeadParam().put("Content-Type","application/json");
    return httpParamEntity;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值