2 Replies Latest reply: Apr 25, 2014 12:00 PM by anton36 RSS

    Proxy Authentication Problem

    anton36

      Hello,

       

      For my job I would to develop a Java application which downloads some data from a webpage and then process them.

      The problem is that I have to authenticate to a proxy server to connect to the internet. As I browsed on the net, there is a possible way for implementation.

       

      Authenticator.setDefault(new Authenticator() {
      
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(username, password);
          }
      });
      
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port);
      
      URL url = new URL(webpage_url);
      URLConnection connection = url.openConnection(proxy);
      connection.addRequestProperty("User-Agent", userAgent);
      connection.setDoOutput(true);
      try (OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream())) {
          osw.write(String.format("email=%s&password=%s", email, password));
          osw.flush(); }
      

       

      Sometimes it works fine, but usually I get the following exception:

       

      java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
          at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2083)
          at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
          at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
          at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
          at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
      
      
      
      

       

      I don't understand what's the problem and why it's not persistent.

      Do anyone have an idea how could I fix it?