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();
  }

}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__0809 返回首页