Dropbox Java: Use Proxy with authentication

送分小仙女□ 提交于 2019-12-11 07:40:04

问题


I want to create my DbxRequestConfig Object with a StandardHttpRequestor, because I need it to use a Proxy.

The Proxy is a http Proxy, Port 80, and needs authentication.

Proxyaddress:    http://myproxy.com
Proxyport:       80
Proxyusername:   username
Proxypassword:   password

So I tried to use the global Java Proxy setup:

System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on

It did not work.

After looking into the StandardHttpRequestor I realized I need to use this Object as well as a Proyx Object:

Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));            
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);

Which is wrong, because it has no authentication.

For authentication, the net and google show me the following. Putting all together, my current code looks like the following:

String ip = "http://myproxy.com";
int port = 80;

final String authUser = "username";
final String authPassword = "password";

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(authUser, authPassword.toCharArray());
    }
});

System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);

Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
return requ;

But this does not work as well.

What am I doing wrong? I can't seem to get the Proxy to work.


回答1:


One problem was the http:// in String ip = "http://myproxy.com";

My current code looks the following, and works sometimes. Sometimes not. I have no idea why. Sometimes I have to reallow the App to be connected to my DropBox Account, because the authKey doesn't come through the proxy...

Well at least I got an example working for you guys having the same trouble. Maybe the rest of the problem is on the proxy side? I'll have a further look into this. But here comes my code:

public HttpRequestor getProxy(){

    if("true".equals(config.getProperty("proxy","false"))){
        String ip = "proxy.myproxy.com";
        int port = 80;

        final String authUser = "username";
        final String authPassword = "password";

        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(authUser, authPassword.toCharArray());
            }
        });

        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));


        HttpRequestor req = new StandardHttpRequestor(proxy);
        return req;
    }
    return null;
}

As you can see I don't use the StandardHttpRequestor anymore. For the Dropbox code it is the following:

HttpRequestor requ = con.getProxy();
if(requ!=null)
    config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString(),requ);
else
    config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString());

As I already said, sometimes it works. Sometimes not. I'm going to write more info about that as soon as I know if it's because of the code or because of the proxy itself.



来源:https://stackoverflow.com/questions/26523360/dropbox-java-use-proxy-with-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!