首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java 设置代理服务器_网络代理

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

作者头像
全栈程序员站长
发布2022-09-30 20:17:22
发布2022-09-30 20:17:22
9530
举报

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
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.cn/195302.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月10日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 该实现方法可以放在多线程的main主线程中,依然起作用!!!
  • 支持真正的http和真正有证书的https请求有效!!!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档