java post请求格式_java post请求调用参数格式为form-data的接口

接口参数使用postman调用如图所示,只能使用form-data格式调用

e7b9534bf07bf888c2a90b6d1b0a2692.png

public static String doPost(String url, HashMapmap) throws Exception {

String result = "";

CloseableHttpClient client = null;

CloseableHttpResponse response = null;

RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(550000).setConnectTimeout(550000)

.setConnectionRequestTimeout(550000).setStaleConnectionCheckEnabled(true).build();

client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();

// client = HttpClients.createDefault();

URIBuilder uriBuilder = new URIBuilder(url);

HttpPost httpPost = new HttpPost(uriBuilder.build());

httpPost.setHeader("Connection", "Keep-Alive");

httpPost.setHeader("Charset", CHARSET_UTF8);

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

Iterator> it = map.entrySet().iterator();

Listparams = new ArrayList();

while (it.hasNext()) {

Map.Entryentry = it.next();

NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());

params.add(pair);

}

httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

try {

response = client.execute(httpPost);

if (response != null) {

HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

result = EntityUtils.toString(resEntity, CHARSET_UTF8);

}

}

} catch (ClientProtocolException e) {

throw new RuntimeException("创建连接失败" + e);

} catch (IOException e) {

throw new RuntimeException("创建连接失败" + e);

}

return result;

}

------------------------------------------------以下为之前的实现方式-------------------------以上为新的思路----------------------

就不废话了直接上调用的代码吧

//import org.apache.commons.httpclient.HttpClient;

//import org.apache.commons.httpclient.methods.PostMethod;

//以上为部分jar包

public static String doPost(String url, String appid,String token,String info) throws Exception {

HttpClient client = new HttpClient();

client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);

client.getHttpConnectionManager().getParams().setSoTimeout(3000);

PostMethod method = new UPostMethod(url);//UPostMethod继承PostMethod,见下所示

String result = "";

try {

method.addParameter("appid",appid);

method.addParameter("accesstoken",token);

method.addParameter("infojson",info);

client.executeMethod(method);

byte[] response = method.getResponseBody();

result = new String(response,"UTF-8");//返回值解析时用的编码格式

} catch (Exception e) {

throw new RuntimeException("创建连接失败" + e);

}finally{

method.releaseConnection();

}

return result;

}

UPostMethod.java

import org.apache.commons.httpclient.methods.PostMethod;

public class UPostMethod extends PostMethod {

public UPostMethod(String url){

super(url);

}

@Override

public String getRequestCharSet(){

return "UTF-8";//设置发送参数的编码格式

}

}

附一个jar包下载的链接:http://mirror.bit.edu.cn/apache/common

这是具体到某一个包的,如果找其他的的话往回返应该就可以吧。

就是在运行的时候可能会报classNotFoundError或者noClassDefFoundError(后面这个不确定准确表达是不是这个,也不想查了emmm)这个问题是因为httpClient需要用到的jar包没有导入,应该就是commons-codec这个jar包,我是下载的一个最新版导入,然后就解决问题的啦。

081bf716ee425dc3d76bf348a85ab3de.png

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值