HttpURLConnection GET/POST写法

本文详细介绍了如何使用原始的HttpURLConnection进行GET、POST表单及字符串请求的方法,并提供了相应的示例代码,旨在帮助开发者在不同场景下灵活选择HTTP连接方式。

现在虽然HttpClient很好使,但也有人在用最原生的HttpURLConnection, 记录一下,备忘之。

public class HttpUrlConnect {
	//get请求
	public String get(String url){
		HttpURLConnection conn = null;
		BufferedReader rd = null ;
	    StringBuilder sb = new StringBuilder ();
	    String line = null ;
	    String response = null;
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			conn.setReadTimeout(20000);
			conn.setConnectTimeout(20000);
			conn.setUseCaches(false);
			conn.connect();
			rd  = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
			while ((line = rd.readLine()) != null ) {
                sb.append(line);
            }
			response = sb.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{		
			try {
				if(rd != null){
					rd.close();
				}
				if(conn != null){
					conn.disconnect();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;
	}
	//post表单请求
	public String post(String url, Map<String, String> form){
		HttpURLConnection conn = null;
		PrintWriter pw = null ;
		BufferedReader rd = null ;
		StringBuilder out = new StringBuilder();
	    StringBuilder sb = new StringBuilder();
	    String line = null ;
	    String response = null;
	    for (String key : form.keySet()) {
	    	if(out.length()!=0){
	    		out.append("&");
	    	}
	    	out.append(key).append("=").append(form.get(key));
		}
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setReadTimeout(20000);
			conn.setConnectTimeout(20000);
			conn.setUseCaches(false);
			conn.connect();
			pw = new PrintWriter(conn.getOutputStream());
			pw.print(out.toString());
			pw.flush();
			rd  = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
			while ((line = rd.readLine()) != null ) {
                sb.append(line);
            }
			response = sb.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{		
			try {
				if(pw != null){
					pw.close();
				}
				if(rd != null){
					rd.close();
				}
				if(conn != null){
					conn.disconnect();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;
	}
	//post字符串请求
	public String post(String url, String rawBody){
		HttpURLConnection conn = null;
		PrintWriter pw = null ;
		BufferedReader rd = null ;
	    StringBuilder sb = new StringBuilder ();
	    String line = null ;
	    String response = null;
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setReadTimeout(20000);
			conn.setConnectTimeout(20000);
			conn.setUseCaches(false);
			conn.connect();
			pw = new PrintWriter(conn.getOutputStream());
			pw.print(rawBody);
			pw.flush();
			rd  = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
			while ((line = rd.readLine()) != null ) {
                sb.append(line);
            }
			response = sb.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{		
			try {
				if(pw != null){
					pw.close();
				}
				if(rd != null){
					rd.close();
				}
				if(conn != null){
					conn.disconnect();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;
	}
	
	public static void main(String[] args) {
		HttpUrlConnect h = new HttpUrlConnect();
		System.out.println(h.get(""));
		Map<String, String> form = new HashMap<String, String>();
		form.put("test", "test");
		System.out.println(h.post("", form));
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值