Dropbox Java:将代理与身份验证配合使用 [英] Dropbox Java: Use Proxy with authentication
问题描述
我想用StandardHttpRequestor
创建我的DbxRequestConfig
对象,因为我需要使用它来使用代理.
I want to create my DbxRequestConfig
Object with a StandardHttpRequestor
, because I need it to use a Proxy.
该代理是一个http代理,端口80,并且需要身份验证.
The Proxy is a http Proxy, Port 80, and needs authentication.
Proxyaddress: http://myproxy.com
Proxyport: 80
Proxyusername: username
Proxypassword: password
所以我尝试使用全局Java代理设置:
So I tried to use the global Java Proxy setup:
System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on
它没有用.
研究了StandardHttpRequestor
之后,我意识到我需要同时使用该对象和Proyx
对象:
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.
对于身份验证,网络和Google向我显示了以下内容.综上所述,我当前的代码如下所示:
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.
推荐答案
一个问题是String ip = "http://myproxy.com";
我当前的代码如下所示,并且有时可以工作.有时不是.我不知道为什么.有时我必须重新设置应用程序才能连接到我的DropBox帐户,因为authKey不会通过代理...
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;
}
如您所见,我不再使用StandardHttpRequestor
.对于Dropbox代码,如下所示:
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.
这篇关于Dropbox Java:将代理与身份验证配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!