Java proxy setup, URL and System.setProperty?

I made a java application that gets response time from the internet. I work because of the proxy server, now I have this problem that I can not understand, my code is disabled here

URL website = new URL("http", proxy, Integer.parseInt(proxyPort), websiteUrl)
BufferedReader in = new BufferedReader(new InputStreamReader(website.openStream()));
long start = System.currentTimeMillis();
while ((in.readLine()) != null){}
long elapsedTimeMillis = System.currentTimeMillis()-start;

Vs

URL website = new URL(websiteUrl);
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
BufferedReader in = new BufferedReader(new InputStreamReader(website.openStream()));
long start = System.currentTimeMillis();
while ((in.readLine()) != null){}
long elapsedTimeMillis = System.currentTimeMillis()-start;

When i use

URL website = new URL(websiteUrl);
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);

I get an average response time of 0.064

and when i use

URL website = new URL("http", proxy, Integer.parseInt(proxyPort), websiteUrl)

I get a much higher response time of 0.219. How can I tell which one gives me the exact time?

+3
source share
2 answers

Check out the Javadoc form you use to create an instance of your URL in the first case, it does not do what you want:

URL (String protocol, String host, int port, String file)
Creates a URL object from the specified protocol, host, port number, and file.

- -, , - -, -

http://proxyhost:3128/http://mysite.com/index.html

... , , , .

+3

. HTTP- -. , HTTP URL InputStream, , .

0

Source: https://habr.com/ru/post/1716795/


All Articles