我想用DbxRequestConfig
创建我的StandardHttpRequestor
对象,因为我需要它来使用代理。
代理服务器是一个http代理服务器,端口80,需要身份验证。
Proxyaddress: http://myproxy.com
Proxyport: 80
Proxyusername: username
Proxypassword: password
所以我尝试使用全局Java代理设置:
System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on
它不起作用。
在查看StandardHttpRequestor
后,我意识到我需要使用此对象以及Proyx
对象:
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
哪个错误,因为它没有身份验证。
对于身份验证,net和google显示以下内容。总而言之,我目前的代码如下所示:
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;
但这不起作用。
我做错了什么? 我似乎无法让代理工作。
答案 0 :(得分:1)
一个问题是http://
String ip = "http://myproxy.com";
我目前的代码看起来如下,并且有时会起作用。有时不是。我不知道为什么。有时我必须将应用程序连接到我的DropBox帐户,因为authKey没有通过代理...
至少我得到了一个例子,为你们解决同样的问题。也许问题的其余部分是在代理方面?我将进一步研究这个问题。但是我的代码来了:
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代码,它是以下内容:
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());
正如我已经说过的,有时候它有效。有时不是。一旦我知道是因为代码还是因为代理本身,我就会写更多相关信息。