我创建了一个Java Lotus Notes代理,该代理调用基于REST的Web服务。该服务是HTTPS,需要用户名和密码。证书已正确安装。
因为这是Lotus Notes代理,所以我仅限于Java 1.6。没有正确设置“授权”属性。设置属性后,实际上未设置该属性,并且不会发生异常。代理失败,因为授权为空。
是否需要执行一些操作才能正确设置属性?我尝试了几种方式对凭据进行编码。我试过只是将其设置为字符串。由于某种原因,该行被忽略。
public String getBlendData(String endpoint, String authString, String username, String password) throws IOException{
URL url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
byte[] message = (username+":"+password).getBytes("UTF-8");
String authStringEnc = javax.xml.bind.DatatypeConverter.printBase64Binary(message);
System.out.println("AuthStringEncoded: " + authStringEnc);
conn.setRequestProperty("Authorization", "Basic " + authStringEnc); //THIS IS THE PROBLEM, this fails silently
System.out.println("auth property: " + conn.getRequestProperty("Authorization")); //returns null
System.out.println("accept property: " + conn.getRequestProperty("Accept")); //returns the correct value
conn.connect();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
return ""; //change later
}