java发送带请求体的delete请求

java发送带请求体的delete请求

有时候,我们需要在A系统中使用delete请求调用B系统中的方法进行批量删除数据,所以需要向请求体中写入参数,但是org.apache.commons.httpclient.methods.DeleteMethod这个类中没有我们所需要的方法。此时,我们需要自己实现一个DeleteMethod。代码如下:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import com.alibaba.fastjson.JSONObject;

public class HttpClientUtil {
	
	public static JSONObject doPost(String url, String params) {
		String respstr = "";
		try {
			System.out.println("请求参数:"+params);
			PostMethod postMethod = new PostMethod(url);
			RequestEntity se = new StringRequestEntity(params, "application/json", "UTF-8");//将请求参数放到RequestEntity 中
			postMethod.setRequestEntity(se);
			HttpClient httpclient = new HttpClient();
			httpclient.executeMethod(postMethod);
			
			InputStream inputStream = postMethod.getResponseBodyAsStream();  
		    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));  
		    StringBuffer stringBuffer = new StringBuffer();  
		    String str= "";  
		    while((str = br.readLine()) != null){  
		    	stringBuffer .append(str );  
		    }
		    respstr=stringBuffer.toString();

		    System.out.println("请求结果:"+respstr);
		}catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject result = JSONObject.parseObject(respstr);
        return result;
    }
	
	public static JSONObject doPut(String url, String params){
		String respstr = "";
		try {
			System.out.println("请求参数:"+params);
			PutMethod postMethod = new PutMethod(url);
			RequestEntity se = new StringRequestEntity(params, "application/json", "UTF-8");
	        postMethod.setRequestEntity(se);
	        HttpClient httpclient = new HttpClient();
	        httpclient.executeMethod(postMethod);
			InputStream inputStream = postMethod.getResponseBodyAsStream();  
		    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));  
		    StringBuffer stringBuffer = new StringBuffer();  
		    String str= "";  
		    while((str = br.readLine()) != null){  
		    	stringBuffer .append(str );  
		    }
		    respstr=stringBuffer.toString();
	        
	        System.out.println("请求结果:"+respstr);
		}catch (Exception e) {
			e.printStackTrace();

		}
		JSONObject result = JSONObject.parseObject(respstr);

        return result;
    }
	
	public static JSONObject doDelete(String url,String params){
		String respstr = "";
		try {
			
			if(params != null) {
				System.out.println("请求参数:"+params);
				DeleteMethodWithBody postMethod = new DeleteMethodWithBody(url);
				RequestEntity se = new StringRequestEntity(params, "application/json", "UTF-8");
				postMethod.setRequestEntity(se);
				HttpClient httpclient = new HttpClient();
				httpclient.executeMethod(postMethod);
				
				InputStream inputStream = postMethod.getResponseBodyAsStream();  
			    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));  
			    StringBuffer stringBuffer = new StringBuffer();  
			    String str= "";  
			    while((str = br.readLine()) != null){  
			    	stringBuffer .append(str );  
			    }
			    respstr=stringBuffer.toString();

//			    System.out.println("请求结果:"+respstr);
			}else {
				DeleteMethod d = new DeleteMethod(url);
				HttpClient httpclient = new HttpClient();
				httpclient.executeMethod(d);
				respstr = d.getResponseBodyAsString();
			}
			
	        System.out.println("请求结果:"+respstr);
		}catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject result = JSONObject.parseObject(respstr);
		
        return result;
    }
	
}


import org.apache.commons.httpclient.methods.EntityEnclosingMethod;

public class DeleteMethodWithBody extends EntityEnclosingMethod {

	@Override
	public String getName() {
		return "DELETE";
	}
	
	public DeleteMethodWithBody() {
	}
	
	public DeleteMethodWithBody(String uri) {
		super(uri);
	}
}

原因分析:
DeleteMethod对象没有setRequestEntity(RequestEntity requestEntity)这个方法,所以无法往请求体中写参数的,此时需要继承EntityEnclosingMethod。PostMethod、PutMethod本来就继承了EntityEnclosingMethod,所以无需自行实现。

注意:
在项目中,不建议使用http协议的请求去修改其他系统的数据,这会造成数据不一致问题。

你可能感兴趣的:(随笔)