本文整理匯總了Java中org.apache.commons.httpclient.HttpState.getCredentials方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpState.getCredentials方法的具體用法?Java HttpState.getCredentials怎麽用?Java HttpState.getCredentials使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.HttpState
的用法示例。
在下文中一共展示了HttpState.getCredentials方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doAuthenticateDefault
import org.apache.commons.httpclient.HttpState; //導入方法依賴的package包/類
private static boolean doAuthenticateDefault(
HttpMethod method,
HttpConnection conn,
HttpState state,
boolean proxy)
throws AuthenticationException {
if (method == null) {
throw new IllegalArgumentException("HTTP method may not be null");
}
if (state == null) {
throw new IllegalArgumentException("HTTP state may not be null");
}
String host = null;
if (conn != null) {
host = proxy ? conn.getProxyHost() : conn.getHost();
}
Credentials credentials = proxy
? state.getProxyCredentials(null, host) : state.getCredentials(null, host);
if (credentials == null) {
return false;
}
if (!(credentials instanceof UsernamePasswordCredentials)) {
throw new InvalidCredentialsException(
"Credentials cannot be used for basic authentication: "
+ credentials.toString());
}
String auth = BasicScheme.authenticate(
(UsernamePasswordCredentials) credentials,
method.getParams().getCredentialCharset());
if (auth != null) {
String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP;
Header header = new Header(s, auth, true);
method.addRequestHeader(header);
return true;
} else {
return false;
}
}