손 으로 쓴 http client

3928 단어 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 라 는 것 을 잊 고 다시 바퀴 를 만 들 었 습 니 다.

좋은 웹페이지 즐겨찾기