• JAVA Post 和 GET请求


    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    class Response {
    
        private final int code;
        private final byte[] data;
    
        Response(int code, byte[] data) {
            this.code = code;
            this.data = data;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder(1024);
            sb.append(code).append("
    ");
            String s = new String(data, StandardCharsets.UTF_8);
            if (s.length() > 1024) {
                sb.append(s.substring(0, 1024)).append("
    ...");
            } else {
                sb.append(s);
            }
            return sb.toString();
        }
    }
    
    public class HttpClient {
    
        public static void main(String[] args) throws Exception {
            Response resp = get("https://www.douban.com");
            System.out.println(resp);
    
            Map<String, String> postMap = new HashMap<>();
            postMap.put("form_email", "test");
            postMap.put("form_password", "password");
            Response postResp = post("https://www.douban.com/accounts/login",
                "application/x-www-form-urlencoded",
                toFormData(postMap));
            System.out.println(postResp);
        }
    
        private static Response get(String theUrl) {
            System.err.println("GET: " + theUrl);
            HttpURLConnection conn = null;
            try {
                URL url = new URL(theUrl);
                conn = (HttpURLConnection) url.openConnection();
                ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
                try (InputStream input = conn.getInputStream()) {
                    byte[] buffer = new byte[1024];
                    for (; ; ) {
                        int n = input.read(buffer);
                        if (n == (-1)) {
                            break;
                        }
                        responseBuffer.write(buffer, 0, n);
                    }
                }
                return new Response(conn.getResponseCode(), responseBuffer.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
        }
    
        private static Response post(String theUrl, String contentType, String contentData) {
            System.err.println("POST: " + theUrl);
            HttpURLConnection conn = null;
            try {
                URL url = new URL(theUrl);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
    
                byte[] postData = contentData.getBytes(StandardCharsets.UTF_8);
                conn.setRequestProperty("Content-Type", contentType);
                conn.setRequestProperty("Content-Length", String.valueOf(postData.length));
    
                try (OutputStream output = conn.getOutputStream()) {
                    output.write(postData);
                }
                ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream();
                try (InputStream input = conn.getInputStream()) {
                    byte[] buffer = new byte[1024];
                    for (; ; ) {
                        int n = input.read(buffer);
                        if (n == (-1)) {
                            break;
                        }
                        responseBuffer.write(buffer, 0, n);
                    }
                }
                return new Response(conn.getResponseCode(), responseBuffer.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
        }
    
        private static String toFormData(Map<String, String> map) throws IOException {
            List<String> list = new ArrayList<>(map.size());
            for (String key : map.keySet()) {
                list.add(key + "=" + URLEncoder.encode(map.get(key), "UTF-8"));
            }
            return String.join("&", list);
        }
    }
  • 相关阅读:
    信道、模拟信道、数字信道、基带信号、宽带信号的概念
    数据、信息、信号与码元的概念
    如何少些重复代码?
    编程中阶段性测试的重要性
    Python 字典的初始化,字典参数引用传递等问题
    什么是操作系统内核?有什么意义?
    什么是系统调用?系统调用的执行过程是什么?
    鼠标右键新建 Typora 文件
    如何快速高效的学习一门新技术
    字符串处理
  • 原文地址:https://www.cnblogs.com/mrma/p/12836226.html
Copyright © 2020-2023  润新知