• HttpURLConnection


    HttpURLConnection

    URL url = new URL(targetUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    
    connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36");
    connection.setRequestProperty("accept-language","zh-CN,zh;q=0.9");
    connection.setRequestProperty("sec-ch-ua-platform","Windows");
    connection.setRequestProperty("sec-ch-ua-platform-version","10.0.0");
    connection.setRequestProperty("sec-ch-ua","'Google Chrome';v='95', 'Chromium';v='95', ';Not A Brand';v='99'");
    connection.setRequestProperty("cookie","");
    
    // 默认就是Get,可以采用post,大小写都行,因为源码里都toUpperCase了。
    connection.setRequestMethod("GET");
    // 是否允许缓存,默认true。
    connection.setUseCaches(Boolean.FALSE);
    // 是否开启输出输入,如果是post使用true。默认是false
    // connection.setDoOutput(Boolean.TRUE);
    // connection.setDoInput(Boolean.TRUE);
    // 设置请求头信息
    connection.addRequestProperty("Connection", "close");
    // 设置连接主机超时(单位:毫秒)
    connection.setConnectTimeout(8000);
    // 设置从主机读取数据超时(单位:毫秒)
    connection.setReadTimeout(8000);
    // 设置Cookie
    // connection.addRequestProperty("Cookie", "你的Cookies");
    // 开始请求
    int index = targetUrl.indexOf("://") + 3;
    String baseUri = targetUrl.substring(0, index) + url.getHost();
    document2 = Jsoup.parse(connection.getInputStream(), "GBK", baseUri);

    #####################

  • 相关阅读:
    WPF MarkupExtension
    WPF Binding小数,文本框不能输入小数点的问题
    WPF UnhandledException阻止程序奔溃
    .Net Core的总结
    C#单元测试
    Csla One or more properties are not registered for this type
    unomp 矿池运行问题随记
    矿池负载运行监测记录
    MySql 数据库移植记录
    后台服务运行后无故停止运行,原因不明
  • 原文地址:https://www.cnblogs.com/herd/p/16119453.html
Copyright © 2020-2023  润新知