zoukankan      html  css  js  c++  java
  • Java使用HttpURLConnection模拟浏览器发送HTTP请求

    package com.albertyy;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    /*
     * 微信公众号:AlbertYang
     */
    public class SendHttp {
      
      public static void main(String[] args) {
        String s1=sendGet("https://www.baidu.com","");
        if(s1!=null) {
          System.out.println(s1);
        }
      }
      
      //发送get请求
      public static String sendGet(String url, String param) {
        StringBuffer result = new StringBuffer();
        String urlName = url + "?" + param;
        BufferedReader in = null;
        try {
          URL realURL = new URL(urlName);
          HttpURLConnection conn = (HttpURLConnection) realURL.openConnection();
          conn.setConnectTimeout(10000);
          conn.setReadTimeout(10000);
          conn.setRequestProperty("accept", "*/*");
          conn.setRequestProperty("connection", "close");
          conn.setRequestProperty("user-agent",
              "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
          conn.connect();
          //        Map<String, List<String>> map = conn.getHeaderFields();
          //        for (String s : map.keySet()) {
          //            System.out.println(s + "-->" + map.get(s));
          //        }
          if (conn.getResponseCode() == 200) {
            in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
              result.append(line);
            }
          }else {
            in = new BufferedReader(
                new InputStreamReader(conn.getErrorStream(), "utf-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
              System.out.println(line);
            }
            return null;
          }
        } catch (IOException e) {
          e.printStackTrace();
          return null;
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
        return result.toString();
      }
      //发送请求
      public static String sendPost(String url, String param) {
        StringBuffer result = new StringBuffer();
        PrintWriter out = null;
        BufferedReader in = null;
        try {
          URL realUrl = new URL(url);
          HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
          conn.setConnectTimeout(10000);
          conn.setReadTimeout(10000);
          conn.setRequestProperty("accept", "*/*");
          conn.setRequestProperty("connection", "close");
          conn.setRequestProperty("user-agent",
              "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
          conn.setDoOutput(true);
          conn.setDoInput(true);
          out = new PrintWriter(conn.getOutputStream());
          out.print(param);
          out.flush();
          if (conn.getResponseCode() == 200) {
            in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
              result.append(line);
            }
          }else {
            in = new BufferedReader(
                new InputStreamReader(conn.getErrorStream(), "utf-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
              System.out.println(line);
            }
            return null;
          }
        } catch (IOException e) {
          e.printStackTrace();
          return null;
        } finally {
          if (out != null) {
            out.close();
          }
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
        return result.toString();
      }
    
    }
    
  • 相关阅读:
    百度Tera数据库介绍——类似cassandra,levelDB
    Cassandra——类似levelDB的基于p2p架构的分布式NOSQL数据库
    Greenplum——升级的分布式PostgresSQL
    Neo4j图数据库简介和底层原理
    445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例
    22. Generate Parentheses——本质:树,DFS求解可能的path
    LevelDb简单介绍和原理——本质:类似nedb,插入数据文件不断增长(快照),再通过删除老数据做更新
    es根据磁盘使用情况来决定是否分配shard
    ES mlockall作用——preventing that memory from being paged to the swap area
    我相信我会反击!围绕艰苦的开局迫使程序员初尝体验
  • 原文地址:https://www.cnblogs.com/yangxianyang/p/13675576.html
Copyright © 2011-2022 走看看