• HttpURLConnection读取http信息


    废话不多说,直接上code.

    package mytest;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Map;
    import java.util.Set;
    import java.io.InputStream;
    
    public class MainEntry {
    
        public static void main(String[] args) throws Exception {
            String urlStr = "http://passport.cnblogs.com/user/signin";
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            
            StringBuffer params = new StringBuffer();
            params.append("input1")
                  .append("=")
                  .append("lucas.shanghai")
                  .append("&")
                  .append("input2")
                  .append("=")
                  .append("abc");
            
            byte[] bytes = params.toString().getBytes();
            PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
            printWriter.write(params.toString());
            printWriter.flush();
            
            Map headers = conn.getHeaderFields();
            Set<String> keys = headers.keySet();
            
            for(String key : keys)
            {
                System.out.println(key + ": " + conn.getHeaderField(key));
            }
            
            int responseCode = conn.getResponseCode();
            
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = "";
            
            StringBuffer responseResult = new StringBuffer();
            while((line = bufferedReader.readLine()) != null) {
                responseResult.append("/n").append(line);
            }
            
            System.out.println(responseResult.toString());
        }
    }
  • 相关阅读:
    JavaScript深拷贝和浅拷贝
    JavaScript正则表达式(一)-常用方法
    面向对象之继承-5种JavaScript继承的方法
    java学习笔记之System类
    MySQL的grant,revoke使用
    LoadRunner+Android模所器录制脚本
    Mysql
    mosquitto_pub和mosquitto_sub 命令参数说明
    【转载】原 IntelliJ IDEA (idea)引入eclipse web项目
    eclipse恢复默认布局
  • 原文地址:https://www.cnblogs.com/devtesters/p/4971792.html
Copyright © 2020-2023  润新知