maven
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> <scope>compile</scope> </dependency>multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
使用
public static String post(String url, String jsonstr, Map<String, String> heads) throws IOException { String result = ""; CloseableHttpResponse httpResponse = null; CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) httpClient = HttpClientBuilder.create().build(); String charset = "utf-8"; // 定义请求的参数 //URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build(); //httpGet = new HttpGet(uri); // 创建Post请求 httpPost = new HttpPost(url); //解析对象 StringEntity entity = new StringEntity(jsonstr, charset); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); if (heads != null) { for (String headKey : heads.keySet()) { httpPost.setHeader(headKey, heads.get(headKey)); } } RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(1000) //连接超时时间 .setConnectionRequestTimeout(1000) //从连接池中取的连接的最长时间 .setSocketTimeout(10 *1000) //数据传输的超时时间 .build(); httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(httpResponse.getEntity(), charset); } result = httpResponse.toString(); }finally { if(httpResponse != null){ try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } try { if(httpPost != null){ httpPost.clone(); } } catch (CloneNotSupportedException e) { e.printStackTrace(); } if(httpClient != null){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return result;配置API RequestConfig()
protected RequestConfigclone() static RequestConfig.Buildercopy(RequestConfig config) static RequestConfig.Buildercustom() intgetConnectionRequestTimeout() 返回从连接管理器请求连接时使用的超时(以毫秒为单位)。 intgetConnectTimeout() 确定连接建立之前的超时时间(以毫秒为单位)。 StringgetCookieSpec() 确定用于HTTP状态管理的cookie规范的名称。 InetAddressgetLocalAddress() 返回用于执行请求的本地地址。 intgetMaxRedirects() 返回要遵循的最大重定向数。 org.apache.http.HttpHostgetProxy() 返回用于请求执行的HTTP代理。 Collection<String>getProxyPreferredAuthSchemes() 确定使用代理主机进行身份验证时支持的身份验证方案的优先顺序。 intgetSocketTimeout() 定义套接字超时(SO_TIMEOUT),以毫秒为单位,这是等待数据的超时,或者换句话说,是两个连续数据包之间的最大时间段不活动。 Collection<String>getTargetPreferredAuthSchemes() 确定使用目标主机进行身份验证时支持的身份验证方案的优先顺序。 booleanisAuthenticationEnabled() 确定是否应自动处理身份验证。 booleanisCircularRedirectsAllowed() 确定是否应允许循环重定向(重定向到同一位置)。 booleanisContentCompressionEnabled() 确定是否请求目标服务器压缩内容。 booleanisDecompressionEnabled() 不推荐使用。 (4.5)使用isContentCompressionEnabled() booleanisExpectContinueEnabled() 确定是否为实体封装方法启用了“期望:100-连续”握手。 booleanisNormalizeUri() 确定客户端是否应规范请求中的URI。 booleanisRedirectsEnabled() 确定是否应自动处理重定向。 booleanisRelativeRedirectsAllowed() 确定是否应拒绝相对重定向。 booleanisStaleConnectionCheckEnabled() 不推荐使用。 (4.4)使用PoolingHttpClientConnectionManager.getValidateAfterInactivity() StringtoString()