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

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

java post请求调用参数格式为form-data的接口_第1张图片

	public static String doPost(String url, HashMap map) 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();
		List params = new ArrayList();
		while (it.hasNext()) {
			Map.Entry entry = 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包,我是下载的一个最新版导入,然后就解决问题的啦。

 

你可能感兴趣的:(java)