我試圖訪問安全URL後面的XML文件。我想從基於已有的XSD綁定的url解組文件。這是我的方法。從Java中的安全URL解編XML
public void urlTest(String url){
URLConnection connection = null;
JAXBContext jc;
try {
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
URL add = new URL(this.url);
connection = add.openConnection();
connection.setRequestProperty("Authorization", "Basic " + encoding);
connection.connect();
jc = JAXBContext.newInstance("XML");
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal(connection.getURL());
System.out.println("Complete");
} catch (IOException ex) {
Logger.getLogger(UrlDownload.class.getName()).log(Level.SEVERE, null, ex);
} catch (JAXBException ex) {
Logger.getLogger(UrlDownload.class.getName()).log(Level.SEVERE, null, ex);
}
}
我從這裏得到一個401服務器錯誤。我傳遞的用戶名和密碼是正確的。也許我錯過了一些東西。
沒有立即浮現在腦海。我會檢查發生了什麼,如果沒有加密的話,用tcpdump檢查一下,如果是的話,會有一些隱藏的技巧。我相信其他人可以提出更復雜的建議。 – Anonymoose 2012-03-13 12:10:37