加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 什么是“proxy.mycompany1.local”

发布时间:2020-12-14 14:26:24 所属栏目:Java 来源:网络整理
导读:我刚刚开始使用 Java网络协议.我试图使用我的代理服务器连接到互联网.当我看到’ https://www.tutorialspoint.com/javaexamples/net_poxy.htm‘的帖子时,他们将http.proxyHost属性设置为’proxy.mycompany1.local’.我知道我可以将其设置为我的代理服务器IP,
我刚刚开始使用 Java网络协议.我试图使用我的代理服务器连接到互联网.当我看到’ https://www.tutorialspoint.com/javaexamples/net_poxy.htm‘的帖子时,他们将http.proxyHost属性设置为’proxy.mycompany1.local’.我知道我可以将其设置为我的代理服务器IP,但我很好奇地知道为什么我的程序仍然可以工作,即使我将其设置为一些随机字符串,如“abcd”.

答:“proxy.mycompany1.local”代表什么?

B.我的程序如何工作,即使我将http.proxyHost设置为“abcd”?

以下是我的工作计划:

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;

public class TestProxy {
    public static void main(String s[]) throws Exception {
        try {

            System.setProperty("http.proxyHost","abcd");
            System.setProperty("http.proxyPort","8080");

            URL u = new URL("http://www.google.com");
            HttpURLConnection con = (HttpURLConnection) u.openConnection();
            System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(false);
        }
        Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI("http://www.google.com")).iterator().next();
        System.out.println("proxy Type : " + proxy.type());
        InetSocketAddress addr = (InetSocketAddress) proxy.address();
        if (addr == null) {
            System.out.println("No Proxy");
        } else {
            System.out.println("proxy hostname : " + addr.getHostName());
            System.out.println("proxy port : " + addr.getPort());
        }
    }
}

这是输出:

200 : OK
proxy Type : HTTP
proxy hostname : abcd
proxy port : 8080

解决方法

首先,根据 System Properties tutorial.

Warning: Changing system properties is potentially dangerous and
should be done with discretion. Many system properties are not reread
after start-up and are there for informational purposes. Changing some
properties may have unexpected side-effects.

而且我的经验表明,当您更改* .proxyHost属性时,您可能会在系统上收到令人不快的问题.所以我不建议你改变这个任务的系统属性.

更好地使用像:

//Proxy instance,proxy ip = 127.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",8080));
conn = new URL(urlString).openConnection(proxy);

和代理授权:

Authenticator authenticator = new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user","mypassword".toCharArray());
        }
    };
    Authenticator.setDefault(authenticator);

现在回到主要问题:

A.“proxy.mycompany1.local”只是例子,你可以使用任何主机名

B.类URL通过Socket使用java.net.PlainSocketImpl类.它尝试解析代理主机名’abcd’,吞下错误并直接转到谷歌.只是试着玩这个代码:

import java.net.*;
import java.io.*;

public class RequestURI {

    public static void main(String[] args) {
        int port = 8181;
        long startTime = System.currentTimeMillis();
        try {
//            System.getProperties().setProperty("http.proxyHost","abcd");
//            System.getProperties().setProperty("http.proxyPort",Integer.toString(port));

            URL url = new URL("http://google.com");
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            int resp = uc.getResponseCode();
            if (resp != 200) {
                throw new RuntimeException("Failed: Fragment is being passed as part of the RequestURI");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Run time in ms ="+ (System.currentTimeMillis() - startTime));
    }
}

当您使用setProperty取消注释部分时,可以看到运行时间更大.不成功的解决主机名的尝试增加了执行时间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读