如何使用HttpURLConnection设置PasswordAuthenticated代理?

时间:2017-06-03 18:56:38

标签: java proxy network-programming httpurlconnection

我正在尝试设置代理连接,并且我希望避免使用系统属性,因为它们将全局应用。当我尝试下面的代码时:

package com.cheyrico2.Test;

import static java.net.Proxy.Type;

import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.util.Scanner;

import org.apache.commons.codec.binary.Base64;

import sun.net.www.protocol.https.Handler;

public class App {
    public static void main(String[] args) {
        URL urlObject = null;
        try {
            urlObject = new URL(null, "https://www.google.com", new Handler());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        final String userName = "proxyUserName";
        final String password = "proxyPassword";
        Authenticator.setDefault(new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password.toCharArray());
            }
        });
        Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("httpProxyServer", 6060));
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) urlObject.openConnection(proxy);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            conn.setRequestMethod("GET");
        } catch (ProtocolException e1) {
            e1.printStackTrace();
        }
        try {
            conn.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream bodyStream = null;
        try {
            int responseCode = conn.getResponseCode();
            if (responseCode >= 400 || responseCode == -1) {
                bodyStream = conn.getErrorStream();
            } else {
                bodyStream = conn.getInputStream();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = getResponse(bodyStream);
        System.out.println(result);
    }

    private static String getResponse(InputStream bodyStream) {
        String result = null;
        Scanner s = new Scanner(bodyStream);
        try {
            s.useDelimiter("\\A");
            result = s.hasNext() ? s.next() : "";
        } finally {
            s.close();
        }
        return result;
    }
}

我收到以下错误。似乎连接无法找到要使用的凭据。

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2124)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at com.cheyrico2.Test.App.main(App.java:54)
Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at java.util.Scanner.<init>(Scanner.java:563)
    at com.cheyrico2.Test.App.main(App.java:69)

我已经使用网络浏览器验证了我的代理服务器连接并且它可以正常工作,但这似乎不起作用。我错过了什么?任何建议都会被高度评价。

0 个答案:

没有答案