第三方接口使用form-data格式请求
public static String doPost(String url, HashMap<String, String> map) {
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(3000);
PostMethod method = new PostMethod(url);//UPostMethod继承PostMethod,见下所示
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
String result = "";
try {
while (it.hasNext()) {
method.addParameter(it.next().getKey(), it.next().getValue());
}
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;
}
参考自:https://blog.csdn.net/lee_master/article/details/91966759