zoukankan      html  css  js  c++  java
  • 一个手写的 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 这个东西,结果又重新造了个轮子  

  • 相关阅读:
    TCP/IP笔记 一.综述
    Makefile的规则
    u盘安装ubuntu10.04 server.txt
    浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC
    cocos2d-js 热更新具体解释(一)
    C#一个托付的样例
    JAVA学习之 异常处理机制
    阿里巴巴校招内推简历筛选方案
    《凑硬币》 动态规划算法入门
    android 读取xml
  • 原文地址:https://www.cnblogs.com/xinsheng/p/4650483.html
Copyright © 2011-2022 走看看