相关文章推荐
爱健身的鼠标垫  ·  在NPOI ...·  1 年前    · 
聪明伶俐的冰棍  ·  UPDATE (Transact-SQL) ...·  1 年前    · 
安静的海龟  ·  关于Mongodb + java + ...·  2 年前    · 

由于项目的需要,在前面记录过利用HttpUrlConnection发送Put请求的方法,链接在这http://blog.csdn.net/crystaldestiny/article/details/46469465,现在,项目中又有需要以Patch方式发送Http请求,真心感觉这两种凡是用得不是很多,网上的资料也很有限,参考过网上一些帖子的后,算是实现了!在此记录一下,本人也是菜鸟,欢迎交流学习!

通过查看HttpUrlConnection的相关资发现,该类应该不支持Patch方式的Http请求,但是通过功能更加强大的Apache  HttpClient却可以轻松实现PUT和PATCH请求,先来看一看,利用HttpClient实现Put请求的代码:

	public static JSONObject executeHttpPut(String url,JSONObject jsonParam){
		JSONObject resultObj = null;
		HttpClient httpClient = new DefaultHttpClient(); 
		HttpPut httpPut = new HttpPut(url);
		httpPut.setHeader("Content-type", "application/json");
		httpPut.setHeader("Charset", HTTP.UTF_8);
		httpPut.setHeader("Accept", "application/json");
		httpPut.setHeader("Accept-Charset", HTTP.UTF_8);
		try {
			if (jsonParam != null) {
				StringEntity entity = new StringEntity(jsonParam.toString(),HTTP.UTF_8);
				httpPut.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPut);
			resultObj = new JSONObject(EntityUtils.toString(response.getEntity()));
		} catch (IOException | ParseException | JSONException e) {
			e.printStackTrace();
		return resultObj;

上面的代码应该很简单,基本上跟利用HttpClient发送Post请求一样,只需要将

HttpPost httpPost = new HttpPost(url); 
<pre name="code" class="java">HttpPut httpPut = new HttpPut(url);
就OK了。
</pre><pre name="code" class="java">对于Patch请求,查看HttpClient的API可以看到,在其给出的API中,并没有所期待的HttpPatch类,也有人说在最新的版本里面好像是有HttpPatch这个类的,应为手上是个Android项目,可能android版本中集成 的HttpClient版本比较低,确实找不到HttpPatch这个类(在java项目中,可以去下载最新的Apache HttpClient 包),但是参考http://www.bubuko.com/infodetail-72930.html这个帖子的做法,完全可以满足需求,即<span style="color: rgb(63, 63, 63); font-family: 'Microsoft YaHei'; font-size: 14px; line-height: 30px;">重写一下 HttpPut 或 HttpPost 的 getMethod 方法</span>
代码如下:
public class HttpPatch extends HttpPut {
    public HttpPatch(String url) {
        super(url);
    @Override
    public String getMethod() {
        return "PATCH";
}
这里要说的是,确实如参考的文章所说,重写HttpPut或者HttpPost的getMethod方法都是一样的,都可以实现。
然后利用上面的HttpPatch类,发送带有
public static JSONObject executeHttpPatch(String url,JSONObject jsonParam){
		JSONObject resultObj = null;
		HttpClient httpClient = new DefaultHttpClient(); 
		HttpPatch httpPatch = new HttpPatch(url);
		httpPatch.setHeader("Content-type", "application/json");
		httpPatch.setHeader("Charset", HTTP.UTF_8);
		httpPatch.setHeader("Accept", "application/json");
		httpPatch.setHeader("Accept-Charset", HTTP.UTF_8);
		try {
			if (jsonParam != null){
				StringEntity entity = new StringEntity(jsonParam.toString(),HTTP.UTF_8);
				httpPatch.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPatch);
			resultObj = new JSONObject(EntityUtils.toString(response.getEntity()));
		} catch (ParseException | JSONException | IOException   e) {
			e.printStackTrace();
		return resultObj;
	}


说明:由于项目需要,传入的参数和返回值都是JSONObject对象。
从代码可以看到,其实重写一下 HttpPut 或 HttpPost 的 getMethod 方法之后,发送Patch请求跟发送Put和Post请求基本上没有什么区别!
欢迎交流学习!
                    由于项目的需要,在前面记录过利用HttpUrlConnection发送Put请求的方法,链接在这http://blog.csdn.net/crystaldestiny/article/details/46469465,现在,项目中又有需要以Patch方式发送Http请求,真心感觉这两种凡是用得不是很多,网上的资料也很有限,参考过网上一些帖子的后,算是实现了!在此记录一下,本人也是菜鸟,欢迎交流学习
	final HttpPatch httpUriRequest = new HttpPatch(url);
	httpUriRequest.setHeader("cookie", "token=" + token);
	httpUriRequest.setHeader("content-type", "application/json;charset=UTF-8");
	httpUriRequ
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
  public static void main(String[] args) throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://example.com/api/post");
    StringEntity entity = new StringEntity("param1=value1&param2=value2");
    httpPost.setEntity(entity);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity responseEntity = response.getEntity();
    System.out.println(EntityUtils.toString(responseEntity));
    response.close();
    httpClient.close();
在上面的代码,我们创建了一个`CloseableHttpClient`对象,并使用`HttpPost`类来发送POST请求请求地址为`http://example.com/api/post`。我们使用`StringEntity`类来封装请求参数,并通过设置请求头来告诉服务器请求内容的类型。最后,我们使用`CloseableHttpResponse`对象获取服务器的响应,并通过`EntityUtils.toString`方法将响应内容转换为字符串。