HttpURLConnection GET和POST请求调用远程接口
package com.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import org.json.JSONObject; public class Test { public static void main(String[] args) { useGetMethod("http://127.0.0.1:8080/getData"); usePostMethod("http://127.0.0.1:8080/getData"); } public static Map<String,Object> useGetMethod(String get_url){ try { Map<String,Object> result = new HashMap<String,Object>(); URL url = new URL(get_url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); //设置连接远程服务器的超时时间 connection.setConnectTimeout(30000); //设置读取远程返回的数据时间 connection.setReadTimeout(30000); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { StringBuffer str = new StringBuffer(); InputStream in = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); try{ String line = ""; while((line = br.readLine()) != null) { str.append(line); } } finally { br.close(); in.close(); } JSONObject jsonObject = new JSONObject(str.toString()); result.put("code", jsonObject.get("code")); result.put("message", jsonObject.get("message")); }else{ System.out.println("错误状态码:"+responseCode); result.put("code", responseCode); result.put("message", "异常"); } connection.disconnect(); return result; } catch (Exception e) { e.printStackTrace(); } return null; } public static Map<String,Object> usePostMethod(String post_url){ try { Map<String,Object> result = new HashMap<String,Object>(); URL url = new URL(post_url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置请求方式 connection.setRequestMethod("POST"); //设置是否向远程服务写入数据 connection.setDoOutput(true); //设置是否从远程服务读取数据 connection.setDoInput(true); //设置是否使用缓存,post请求不能使用缓存 connection.setUseCaches(false); //设置连接主机服务器超时时间 connection.setConnectTimeout(30000); //设置读取主机服务器返回数据超时时间 connection.setReadTimeout(30000); //设置本次连接是否自动重定向 connection.setInstanceFollowRedirects(true); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); //设置接收数据的格式 connection.setRequestProperty("Accept", "application/json"); //设置请求参数的数据格式 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置与服务器保持连接 connection.addRequestProperty("Connection","Keep-Alive"); connection.setRequestProperty("Accept-Language", "zh-CN,zh;0.9"); //连接 connection.connect(); //获取响应 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //post参数 String content = "name="+URLEncoder.encode("小明", "utf-8")+"&password=123"; out.writeBytes(content); //out.write(content); //响应码 int responseCode = connection.getResponseCode(); //200 if (responseCode == HttpURLConnection.HTTP_OK) { StringBuffer str = new StringBuffer(); InputStream in = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); try{ String line = ""; while((line = br.readLine()) != null) { str.append(line); } } finally { br.close(); in.close(); } JSONObject jsonObject = new JSONObject(str.toString()); result.put("code", jsonObject.get("code")); result.put("message", jsonObject.get("message")); }else{ result.put("code", responseCode); result.put("message", "异常"); } //关闭流 out.flush(); out.close(); //断开连接 connection.disconnect(); return result; } catch (Exception e) { e.printStackTrace(); } return null; } }