Java实现HTTP请求
import org.apache.commons.lang3.StringUtils; import java.io.*; import java.net.*; public class HTTPUtil { public static String HTTPGet(String url, String param) { InputStream inputStream = null; BufferedReader bufferedReader = null; HttpURLConnection connection = null; StringBuffer stringBuffer = null; try { String urlNameString = url + (StringUtils.isBlank(param) ? "" : ("?" + param)); //创建连接 URL realUrl = new URL(urlNameString); // URL url = new URL("http://192.168.31.179:19303/entity-area/queryAreaList"); //通过远程url连接对象打开一个连接,强转成httpURLConnection类 connection = (HttpURLConnection) realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 设置连接方式:get connection.setRequestMethod("GET"); // 设置连接主机服务器的超时时间:15000毫秒 connection.setConnectTimeout(15000); // 设置读取远程返回的数据时间:60000毫秒 connection.setReadTimeout(60000); // 发送请求 connection.connect(); //获取http响应code值 if (connection.getResponseCode() == 200) { // 通过connection连接,获取输入流 inputStream = connection.getInputStream(); // 封装输入流inputStream,并指定字符集 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); stringBuffer = new StringBuffer(); String temp = null; while ((temp = bufferedReader.readLine()) != null) { stringBuffer.append(temp); stringBuffer.append("\r\n"); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { System.err.println(e.getCause().getMessage()); } } if (null != inputStream) { try { bufferedReader.close(); } catch (IOException e) { System.err.println(e.getCause().getMessage()); } } connection.disconnect();// 关闭远程连接 } return null == stringBuffer ? "" : stringBuffer.toString(); } public static String HTTPPost(String url, String param) { HttpURLConnection connection = null; StringBuffer stringBuffer = null; InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { URL realUrl = new URL(url); connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestMethod("POST"); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // URL连接可以用于输入和/或输出。如果想使用URL连接作为输入,设置DoInput *标志为true,如果不是假的。默认为true connection.setDoOutput(true); connection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter out = new PrintWriter(connection.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); //发送请求 connection.connect(); inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); bufferedReader = new BufferedReader(inputStreamReader); stringBuffer = new StringBuffer(); String temp = null; while ((temp = bufferedReader.readLine()) != null) { stringBuffer.append(temp); stringBuffer.append("\r\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStreamReader) { try { inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != connection) { //关闭远程连接 connection.disconnect(); } } return stringBuffer.toString(); } /* public static void main(String[] args) { // String param = "¶m=param"; // String s = HTTPUtil.HTTPGet("http://IP:端口/地址", param); // String s = HTTPUtil.HTTPPost("http://IP:端口/地址", param); System.out.println(s); } */ }