第三方接口使用form-data格式请求

第三方接口使用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

上一篇:Dynamics CRM 2015/2016新特性之二十三:使用Web API执行函数


下一篇:linux下java发送post请求以及安全证书问题