【问题标题】:java system properties for proxy fails on ubuntu代理的 java 系统属性在 ubuntu 上失败
【发布时间】:2013-04-07 18:42:51
【问题描述】:

我在我的 java 代码中使用 system.properties。 它在 Windows 7 操作系统上完美运行,但在 ubuntu12.04 上失败。

我在这两个地方都使用过tomcat。 在这方面的任何帮助都会有所帮助。

System.setProperty("http.proxyHost", proxyhost);
System.setProperty("http.proxyPort", proxyport);        
String encoded = new String(encoder.encode(new String(username+":"+password).getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);

例外: java.io.IOException:服务器返回 HTTP 响应代码:401 用于 URL:http://

【问题讨论】:

  • stackoverflow.com/questions/10479434/… 这可能会有所帮助
  • 如果没有定义的字符集,getBytes() 方法将使用平台的默认字符集返回byte[]。在此过程中可能会丢失信息,并将错误的 base64 编码 BASIC auth 发送到代理。我会先检查一下。
  • 谢谢大家只缺少 UTF-8

标签: java ubuntu proxy


【解决方案1】:

401 错误代码表示“未经授权”。你可以试试这个:

System.setProperty("http.proxyHost", "yourproxyhost");
System.setProperty("http.proxyPort", "yourproxyport");

String credentials = username + ":" + password;
String encoded  = Base64Converter.encode(credentials.getBytes("UTF-8"));
URLConnection uc = url.openConnection();
uc.setRequestProperty("Proxy-Authorization", String.format("Basic %s", encoded));

【讨论】:

  • 谢谢,只缺少 UTF-8
猜你喜欢
  • 1970-01-01
  • 2013-11-29
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 2015-06-14
  • 2015-11-15
  • 1970-01-01
相关资源
最近更新 更多