java 设置代理服务器_网络代理

java 设置代理服务器_网络代理importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.*;importjava.util.Base64;publicclassTest{publicstaticvoidmain(String[]arg…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Base64;

public class Test {
    public static void main(String[] args) {
        try {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            System.setProperty("http.proxyHost", "82.157.163.144");
            System.setProperty("http.proxyPort", "8080");
            System.setProperty("https.proxyHost", "82.157.163.144");
            System.setProperty("https.proxyPort", "8080");
            URL realUrl = new URL("https://www.ljxwtl.cn");
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestMethod("GET");
            // 获取URLConnection对象对应的输出流
            // flush输出流的缓冲
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


class BasicAuthenticator extends Authenticator {
    String userName;
    String password;

    public BasicAuthenticator(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    /**
     * Called when password authorization is needed. Subclasses should
     * override the default implementation, which returns null.
     *
     * @return The PasswordAuthentication collected from the
     * user, or null if none is provided.
     */
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password.toCharArray());
    }
}

该实现方法可以放在多线程的main主线程中,依然起作用!!!

支持真正的http和真正有证书的https请求有效!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195302.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sensor接口之DVP

    sensor接口之DVPsensor接口之DVP什么是DVPDVP时序PCLK、HSYNC、VSYNC对应关系sensor并行输出说明实际测量DVP信号什么是DVPDVP(DigitalVideoPort)是传统的sensor输出接口,采用并行输出方式,d数据位宽有8bit、10bit、12bit、16bit,是CMOS电平信号(重点是非差分信号),PCLK最大速率为96MHz,接口如下图:PCLK:pix…

    2022年6月11日
    62
  • cpu缓冲区大小怎么设置_FL Studio中音频ASIO4ALL的设置

    cpu缓冲区大小怎么设置_FL Studio中音频ASIO4ALL的设置上期我们讲解了FLStudio中音频的相关设置,今天我们来进一步讲解音频设置中的ASIO4ALL的设置,FLStudio安装包括FLStudioASIO和第三方ASIO驱动程序ASIO4ALL,那么大家是否知道ASIO有什么优点吗?速度优势:ASIO驱动程序(通常)允许比标准Windows(“主声音驱动程序”,WDM驱动程序)更低的CPU开销和更低的缓冲区设置。如果您的音频设备具有本机AS…

    2022年5月21日
    65
  • 基于SSM的景区旅游管理系统

    基于SSM的景区旅游管理系统项目运行截图首页展示信息攻略管理酒店管理技术描述开发工具:idea/eclipse数据库:mysqlJar包仓库:Maven前段框架:Html/Thymeleaf后端框架:spring+springmvc+mybatis+SpringBoot资料说明基于SSM的景区旅游管理系统,整体包含前后台页面,整体采用SSM框架,SpringBoot作为基础框架,前端页面采用Thymele

    2022年5月8日
    59
  • pycharm中pip安装地址更改_pycharm怎么安装pip库

    pycharm中pip安装地址更改_pycharm怎么安装pip库pycharm修改pip源为清华源-提高下载速度让PIP源使用国内镜像,提升下载速度和安装成功率。以后文章更新在https://oldpan.me国内pip源:清华大学https://pypi.tuna.tsinghua.edu.cn/simple/在使用pycharm时,…

    2022年8月28日
    0
  • Python & 机器学习之项目实践

    Python & 机器学习之项目实践

    2021年11月21日
    43
  • API Explorer 公测发布

    API Explorer 公测发布

    2021年6月13日
    152

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号