multipart/form-data格式的post请求

String url = "http://xxx";
Map<String, Object> params = new HashMap<>();
params.put("app_id", app_id);
params.put("app_token", app_token);
params.put("domain", domain);

// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
String resultString = "";
PrintWriter out = null;
try {
    // 创建Http Post请求
    HttpPost httpPost = new HttpPost(url);
    // 创建参数列表
    if (params != null) {
        List<NameValuePair> paramList = new ArrayList<>();
        for (String key : params.keySet()) {
            paramList.add(new BasicNameValuePair(key, params.get(key).toString()));
        }
        // 模拟表单
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
        httpPost.setEntity(entity);
    }
    // 执行http请求
    httpResponse = httpClient.execute(httpPost);
    resultString = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
    JSONObject json = JSONObject.fromObject(resultString);
    out = response.getWriter();
    out.append(jsonStr);
    out.flush();
} catch (Exception e) {
	logger.error("xxx出错", e);
    e.printStackTrace();
} finally {
    if (out != null) {
        out.close();
    }
    try {
    	httpResponse.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值