提交工具类

This commit is contained in:
zhanghongqing
2018-09-10 09:30:09 +08:00
parent c29d8c9e8c
commit 82d1de6d6d

View File

@@ -32,9 +32,11 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException; 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.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
@@ -129,6 +131,54 @@ public class HttpClientUtil {
// } // }
return content; 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<NameValuePair> nvps = new ArrayList <NameValuePair>();
// Set<String> 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;
}
/** /**