import java.net.Authenticator; import java.net.PasswordAuthentication; public class fanfouauth extends Authenticator { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("用户名","密码".toCharArray()); } } 使用时 以饭否的API为例,每一次在使用URL的时候,调用Authenticator.setDefault一下.就可以了~ Authenticator.setDefault(new fanfouauth()); URL url = new URL("http://api.fanfou.com/account/verify_credentials.json"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); StringBuffer sb = new StringBuffer(""); String str=""; while((str = reader.readLine())!= null) sb.append(str); return sb.toString();