2013-10-30 26 views
1

我正在嘗試使用java的URL對象進行已認證的請求。問題是我的密碼中有@符號(現在不能更改它),所以當java嘗試url.getUserInfo()時,它會在密碼中停止@,並將我的請求鎖定。我試着用%40來轉義它,但從我在調試模式中看到的它只能看到%40而不是@字符。轉義密碼中的@字符

例子:http://user:[email protected]@stackoverflow.com將用戶信息作爲user:p

+0

這是URL方案中的一個失敗,以說明這種特定情況。你絕對沒有辦法。 –

回答

1

而不是將用戶名和密碼,您可以註冊一個Authenticator,在需要時可以提供憑據。

Authenticator.setDefault(new Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
    if("stackoverflow.com".equals(getRequestingHost()) { 
     return new PasswordAuthentication("user", "[email protected]".toCharArray()); 
    } else { 
     return null; 
    } 
    } 
});