• HttpURLConnection GET/POST写法


    现在虽然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));
    	}
    	
    }
    
  • 相关阅读:
    顾问和注解
    正则
    GitHub 的简单使用
    JavaScript变态题目
    常用的Javascript设计模式
    HTML5 本地裁剪上传图片
    webpack 打包
    详解js闭包
    常用的Javascript设计模式
    call appiy
  • 原文地址:https://www.cnblogs.com/zhangfei/p/5329320.html
Copyright © 2020-2023  润新知