httpcilent

package com.bootdo.common.utils;

import java.io.IOException;
import java.util.;
import com.bootdo.common.domain.HttpParamEntity;
import com.bootdo.common.domain.OsHttpDelete;
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) {

    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;

    CloseableHttpClient httpClient = HttpClients.createDefault();

    CloseableHttpResponse httpResponse = null;

    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("get")){
      //  httpRquest = new HttpGet(httpParamEntity.getUrl());
    }else if (type.equals("patch")){
        httpRquest = new HttpPatch(httpParamEntity.getUrl());
    }else if(type.equals("delete")){
        httpRquest = new OsHttpDelete(httpParamEntity.getUrl());
        // httpDelete.set
    }



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

    try {
        if(httpParamEntity.getBody()!=null){
            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 void main(String[] args) {
HttpParamEntity httpParamEntity=new HttpParamEntity();
httpParamEntity.getHeadParam().put(“cpid”,“167”);
httpParamEntity.getHeadParam().put(“key”,“6f5a3292c3c4731aec91df0a0581c278”);
httpParamEntity.getHeadParam().put(“Content-Type”,“application/json”);
httpParamEntity.setUrl(“http://api.jiai.pro:8080/jiai/familyNumber/3”);
httpParamEntity.getHeadParam().put(“imei”,“356191943756372”);
DeviceDO deviceDO = new DeviceDO();
deviceDO.setImei(“356191943756372”);
httpParamEntity.setBody(JSON.toJSONString(deviceDO));
String result = HttpClientUtil.sendUrl(httpParamEntity, “delete”);
System.out.println(“result:”+result);
}*/
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想发送一个包含多个表单参数的 HTTP 请求,可以将这些参数打包成一个数组,并将该数组作为请求体的一部分发送。具体而言,你可以使用以下方法: 1. 对于 GET 请求,将参数数组中的每个元素添加到 URL 的查询字符串中。例如,如果你有一个名为 `params` 的数组,其中包含两个参数 `foo` 和 `bar`,你可以构造以下 URL: ``` http://example.com/path?foo=value1&bar=value2 ``` 2. 对于 POST 请求,将参数数组编码为表单数据格式,并将其包含在请求体中。你可以使用 `application/x-www-form-urlencoded` MIME 类型来编码表单数据。例如,如果你有一个名为 `params` 的数组,其中包含两个参数 `foo` 和 `bar`,你可以将其编码为以下字符串: ``` foo=value1&bar=value2 ``` 然后将该字符串作为请求体发送。如果你使用的是 HTTP 客户端库,通常会有一个选项来指定请求体的内容类型和编码方式。例如,使用 Python 的 `requests` 库,你可以这样发送一个带有表单参数的 POST 请求: ```python import requests url = 'http://example.com/path' params = {'foo': 'value1', 'bar': 'value2'} response = requests.post(url, data=params) ``` 在这个例子中,`params` 是一个字典,它包含要发送的表单参数。`requests.post()` 方法会将该字典编码为表单数据格式,并将其包含在请求体中。如果你想使用其他编码方式,例如 JSON,你可以将参数字典编码为 JSON 字符串,并将其包含在请求体中,同时设置请求头的 `Content-Type` 为 `application/json`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值