zoukankan      html  css  js  c++  java
  • 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());
        }
    }
  • 相关阅读:
    PHP5.6 和PHP7.0区别
    怎么清除火狐浏览器的cookie?
    PHP 7.0新增特性详解
    一个较好的基础的数据库连接池知识 规格严格
    Iptables 规格严格
    收藏一个Man网址 规格严格
    AIX配置SNMP V3 规格严格
    Tomcat Firewall JMX RMI 规格严格
    在来一个IPTables 规格严格
    java.lang.ClassNotFoundException: listeners.ContextListener . 规格严格
  • 原文地址:https://www.cnblogs.com/devtesters/p/4971792.html
Copyright © 2011-2022 走看看