一个手写的 http client

public class HTTPClient {
	
	public static final String GET = "GET";
	public static final String POST = "POST";
	public static final String PUT = "PUT";
	public static final String DELETE = "DELETE";
	
	public static String getData(String path) throws Exception {
		String responseData = visitWithoutParam(path, GET);
		return responseData;
	}
	
	public static String postData(String path, String data) throws Exception {
		String responseData = visitWithParam(path, POST, data);
		return responseData;
	}
	
	public static String putData(String path, String data) throws Exception {
		return "To do put data";
	}
	
	public static String deleteData(String path) throws Exception {
		return "To do delete data";
	}
	
	public static String visitWithParam(String path, String method, String body) throws Exception{
		InputStream inputStream = null;
		BufferedReader bufferedReader = null;
		
		try {
			URL url = new URL(path);
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setDoInput(true);
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setUseCaches(false);
			
			httpURLConnection.setRequestMethod(method);
			httpURLConnection.setRequestProperty("Content-Type", "application/json");
			httpURLConnection.setRequestProperty("charset", "utf-8");
			httpURLConnection.setRequestProperty("Content-Length", Integer.toString(body.getBytes().length));
			
			DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
			dataOutputStream.writeBytes(body);
			dataOutputStream.flush();
			dataOutputStream.close();
			
			inputStream = httpURLConnection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			StringBuilder stringBuilder = new StringBuilder();
			String line = null;
			
			while((line = bufferedReader.readLine()) != null)
				stringBuilder.append(line);
			
			return stringBuilder.toString();
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			// no need to change null actually
			try {
				if(bufferedReader != null) bufferedReader.close(); 
				if(inputStream != null) inputStream.close();
			} catch (Exception e){
				
			}
		}
	}
	
	public static String visitWithoutParam(String path, String method) throws Exception {
		InputStream inputStream = null;
		BufferedReader bufferedReader = null;
		
		URL url;
		try {
			url = new URL(path);
			HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setDoInput(true);
			httpURLConnection.setUseCaches(false);
			
			httpURLConnection.setRequestMethod(method);
			
			inputStream = httpURLConnection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			
			StringBuilder stringBuilder = new StringBuilder();
			String line = null;
			
			while((line = bufferedReader.readLine()) != null)
				stringBuilder.append(line);
			
			return stringBuilder.toString();
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			try {
				if(bufferedReader != null) bufferedReader.close();
				if(inputStream != null) inputStream.close();
			} catch (Exception e) {
				
			}
		}
		
	}
}

 

自己很久以前写过的一段代码,当时忘记了 apache.httpclient 这个东西,结果又重新造了个轮子  

posted @ 2015-07-16 10:30  SangS  阅读(510)  评论(0编辑  收藏  举报