From 82d1de6d6d32497e7c08e46d42e69b99b74b7c4a Mon Sep 17 00:00:00 2001 From: zhanghongqing Date: Mon, 10 Sep 2018 09:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nis/util/httpclient/HttpClientUtil.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/com/nis/util/httpclient/HttpClientUtil.java b/src/main/java/com/nis/util/httpclient/HttpClientUtil.java index 9553071d7..e07445cf0 100644 --- a/src/main/java/com/nis/util/httpclient/HttpClientUtil.java +++ b/src/main/java/com/nis/util/httpclient/HttpClientUtil.java @@ -32,9 +32,11 @@ import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.conn.ConnectTimeoutException; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClients; @@ -129,6 +131,54 @@ public class HttpClientUtil { // } return content; } + /** + * 处理patch请求. + * @param url 请求路径 + * @param params 参数 + * @return json + * @throws IOException + * @throws ClientProtocolException + */ + public static String patch(String url,String param) throws Exception{ + //实例化httpClient + CloseableHttpClient httpclient = HttpClients.createDefault(); + //实例化patch方法 + HttpPatch httpPatch = new HttpPatch(url); + + httpPatch.setHeader("Content-type", "application/json"); + + httpPatch.setHeader("Charset", "utf-8"); + + httpPatch.setHeader("Accept", "application/json"); + + httpPatch.setHeader("Accept-Charset", "utf-8"); + + //处理参数 +// List nvps = new ArrayList (); +// Set keySet = params.keySet(); +// for(String key : keySet) { +// nvps.add(new BasicNameValuePair(key, params.get(key))); +// } + //结果 + CloseableHttpResponse response = null; + String content=""; + try { + //提交的参数 +// UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8"); + StringEntity entity = new StringEntity(param, "utf-8"); + //将参数给post方法 + httpPatch.setEntity(entity); + //执行post方法 + response = httpclient.execute(httpPatch); + int status = response.getStatusLine().getStatusCode(); + if(status==200){ + content = EntityUtils.toString(response.getEntity()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return content; + } /**