Advertisement
jovenb

Java HTTP Get request

May 26th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. private String executeGet(final String https_url, final String proxyName, final int port) {
  2.     String ret = "";
  3.  
  4.     URL url;
  5.     try {
  6.  
  7.         HttpsURLConnection con;
  8.         url = new URL(https_url);
  9.  
  10.         if (proxyName.isEmpty()) {  
  11.             con = (HttpsURLConnection) url.openConnection();
  12.         } else {                
  13.             Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyName, port));
  14.             con = (HttpsURLConnection) url.openConnection(proxy);
  15.             Authenticator authenticator = new Authenticator() {
  16.                 public PasswordAuthentication getPasswordAuthentication() {
  17.                         return (new PasswordAuthentication(USERNAME, PASSWORD.toCharArray()));
  18.                     }
  19.                 };
  20.             Authenticator.setDefault(authenticator);
  21.         }
  22.  
  23.         ret = getContent(con);
  24.  
  25.     } catch (MalformedURLException e) {
  26.         e.printStackTrace();
  27.     } catch (IOException e) {
  28.         e.printStackTrace();
  29.     }
  30.  
  31.     return ret;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement