Java HTTP请求
目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现。另一种则是通过HttpURLConnection去实现,HttpURLConnection是JAVA的标准类,是JAVA比较原生的一种实现方式。
本文是通过HttpURLConnection的方式发生HTTP请求,总结一下分享给大家,也方便自己以后使用,话不多说上代码。
import java.io.*; import java.net.*; import java.util.Map; /** * Date: 2019-02-25 * * @author * @version 1.0 * Http工具类 */ public class HttpUtil { /** * 使用get请求获取数据 * @param httpUrl * @param params 请求参数,参数为K-V键值对的方式传入 * @return */ public static String HttpGet(String httpUrl, Map<String, String> params) { //链接 HttpURLConnection connection=null; InputStream is=null; BufferedReader br = null; StringBuffer result=new StringBuffer(); try { String parameters = ""; boolean hasParams = false; for(String key : params.keySet()){ String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key +"="+ value +"&"; hasParams = true; } if(hasParams){ parameters = parameters.substring(0, parameters.length()-1); httpUrl += "?" + parameters; } //创建连接 URL url=new URL(httpUrl); connection= (HttpURLConnection) url.openConnection(); //设置请求方式 connection.setRequestMethod("GET"); //设置连接超时时间 connection.setConnectTimeout(15000); //设置读取超时时间 connection.setReadTimeout(15000); //开始连接 connection.connect(); //获取响应数据 if(connection.getResponseCode()==200){ //获取返回的数据 is=connection.getInputStream(); if(is!=null){ br=new BufferedReader(new InputStreamReader(is,"UTF-8")); String temp = null; while ((temp=br.readLine())!=null){ result.append(temp); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } connection.disconnect();// 关闭远程连接 } return result.toString(); } /** * post请求 * @param httpUrl 链接 * @param params 参数 * @return */ public static String HttpPost(String httpUrl, Map<String, String> params) { StringBuffer result=new StringBuffer(); //连接 HttpURLConnection connection=null; OutputStream os=null; InputStream is=null; BufferedReader br=null; try { String parameters = ""; boolean hasParams = false; for(String key : params.keySet()){ String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key +"="+ value +"&"; hasParams = true; } if(hasParams){ parameters = parameters.substring(0, parameters.length()-1); //httpUrl += "?" + parameters; } //创建连接对象 URL url=new URL(httpUrl); //创建连接 connection= (HttpURLConnection) url.openConnection(); //设置请求方法 connection.setRequestMethod("POST"); //设置连接超时时间 connection.setConnectTimeout(15000); //设置读取超时时间 connection.setReadTimeout(15000); //设置是否可读取 connection.setDoOutput(true); //设置响应是否可读取 connection.setDoInput(true); //设置参数类型 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //拼装参数 if(parameters!=null&&!parameters.equals("")){ //设置参数 os=connection.getOutputStream(); //拼装参数 os.write(parameters.getBytes("UTF-8")); } //设置权限 //设置请求头等 //开启连接 connection.connect(); //读取响应 if(connection.getResponseCode()==200){ is=connection.getInputStream(); if(is!=null){ br=new BufferedReader(new InputStreamReader(is,"UTF-8")); String temp=null; while ((temp=br.readLine())!=null){ result.append(temp); } } } //关闭连接 } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } //关闭连接 connection.disconnect(); } return result.toString(); } }