URLConnection应用 - pingdongyi/blog-2 GitHub Wiki

public class InetTest {
    public static String sendGet(String url, String params) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlName = url + "?" + params;
            URL urlObj = new URL(urlName);
            URLConnection connection = urlObj.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            //建立实际连接
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                System.out.println(entry.getKey() + "-->" + entry.getValue());
            }
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String sendPost(String url, String params) {
        PrintWriter out=null;
        BufferedReader in=null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection connection=realUrl.openConnection();
            connection.setRequestProperty("accept","*/*");
            connection.setRequestProperty("connection","Keep-Alive");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            out = new PrintWriter(connection.getOutputStream());
            out.print(params);
            out.flush();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    public static void main(String[] args) {
        String s = InetTest.sendGet("http://localhost:8080/web/user/list", null);
        System.out.println(s);

        s = InetTest.sendPost("http://localhost:8080/web/user/save", "name=aa&buc_userid=111&empid=111");
        System.out.println(s);
    }
}
⚠️ **GitHub.com Fallback** ⚠️