Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. public Proxy getProxyAuthorizationInstance(){
  2.  
  3.         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
  4.  
  5.         Authenticator authenticator = new Authenticator() {
  6.  
  7.             @Override
  8.             public PasswordAuthentication getPasswordAuthentication() {
  9.                 return (new PasswordAuthentication(username,
  10.                         password.toCharArray()));
  11.             }
  12.         };
  13.         Authenticator.setDefault(authenticator);
  14.         System.out.println(host + " " + port + " " + username + " " + password);
  15.         return proxy;
  16.     }
  17. private String sendRequestToGetIPData(){
  18.         HttpURLConnection con = null;
  19.         StringBuilder json = new StringBuilder();
  20.         MyProxy proxy = new MyProxy("", 0, "", "");
  21.         try {
  22.             URL url = new URL("https://ipapi.co/json");
  23.             con = (HttpURLConnection) url.openConnection(proxy.getProxyAuthorizationInstance());
  24.             con.setRequestMethod("POST");
  25.             con.setConnectTimeout(10000);
  26.             con.setReadTimeout(10000);
  27.  
  28.             int responseCode = con.getResponseCode();
  29.             appendToTextArea("Sending " + con.getRequestMethod() + " request to URL : " + url);
  30.             appendToTextArea("Response Code : " + responseCode);
  31.             BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  32.             String inputLine;
  33.             json = new StringBuilder();
  34.             while ((inputLine = in.readLine()) != null) {
  35.                 json.append(inputLine);
  36.             }
  37.             in.close();
  38.  
  39.         } catch (Exception e){
  40.             appendToTextArea(e.getLocalizedMessage() + "\n" + proxy.getHost() + " : " + proxy.getPort() + " " + proxy.getUsername() + " " + proxy.getPassword());
  41.             e.printStackTrace();
  42.         } finally {
  43.             if (con != null) {
  44.                 con.disconnect();
  45.             }
  46.         }
  47.         return json.toString();
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement