Created
May 20, 2011 01:21
-
-
Save iwazer/982162 to your computer and use it in GitHub Desktop.
JavaでBasic認証(ただしマルチスレッドなサーバでは使えない…)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.Authenticator; | |
import java.net.PasswordAuthentication; | |
// Basic認証のAuthenticatorを作成 | |
class HttpAuthenticator extends Authenticator { | |
public String username; | |
public String password; | |
public HttpAuthenticator(String username, String password) { | |
this.username = username; | |
this.password = password; | |
} | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(username, password.toCharArray()); | |
} | |
} | |
// 使う | |
HttpAuthenticator httpAuth = new HttpAuthenticator(user, pass); | |
Authenticator.setDefault(info.httpAuth); | |
URLConnection conn = url.openConnection(); | |
conn.setConnectTimeout(CONNECT_TIMEOUT); | |
conn.setReadTimeout(READ_TIMEOUT); | |
conn.connect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment