Created
January 26, 2016 14:33
-
-
Save madbonez/0e3b2768373e744bd359 to your computer and use it in GitHub Desktop.
Java Proxy system set property and Use Authenticator if it needed/ Then test http connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.bis.integration.regognition; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.Authenticator; | |
import java.net.HttpURLConnection; | |
import java.net.PasswordAuthentication; | |
import java.net.URL; | |
public class JavaBehindProxy { | |
public static final String API_URL = "https://api.github.com"; | |
public static void main(String... args) throws IOException { | |
System.getProperties().put("http.proxyHost", "proxy"); | |
System.getProperties().put("http.proxyPort", "3128"); | |
System.getProperties().put("https.proxyHost", "proxy"); | |
System.getProperties().put("https.proxyPort", "3128"); | |
final String authUser = "user"; | |
final String authPassword = "password"; | |
Authenticator.setDefault( | |
new Authenticator() { | |
public PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication( | |
authUser, authPassword.toCharArray()); | |
} | |
} | |
); | |
/* System.getProperties().put("http.proxyUser", authUser); | |
System.getProperties().put("http.proxyPassword", authPassword);*/ | |
URL urlObj = new URL(API_URL); | |
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); | |
connection.setRequestMethod("GET"); | |
BufferedReader inputBuffer = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
StringBuilder response = new StringBuilder(); | |
String inputLine; | |
while( (inputLine = inputBuffer.readLine()) != null){ | |
response.append(inputLine); | |
} | |
System.out.println(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment