本文整理汇总了Java中org.apache.commons.httpclient.methods.GetMethod.setDoAuthentication方法的典型用法代码示例。如果您正苦于以下问题:Java GetMethod.setDoAuthentication方法的具体用法?Java GetMethod.setDoAuthentication怎么用?Java GetMethod.setDoAuthentication使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.httpclient.methods.GetMethod
的用法示例。
在下文中一共展示了GetMethod.setDoAuthentication方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryAuthentication
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
private static void tryAuthentication(RemoteFileReference wsReference) throws Exception {
URL urlToConnect = wsReference.getUrl();
String wsdlUrl = wsReference.getUrlpath();
String username = wsReference.getAuthUser();
String password = wsReference.getAuthPassword();
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope(urlToConnect.getHost(), urlToConnect.getPort()),
new UsernamePasswordCredentials(username, password)
);
GetMethod get = new GetMethod(wsdlUrl);
get.setDoAuthentication( true );
int statuscode = client.executeMethod(get);
if (statuscode == HttpStatus.SC_UNAUTHORIZED) {
throw new Exception(HttpStatus.SC_UNAUTHORIZED + " - Unauthorized connection!");
}
}
示例2: buildGetMethod
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
/**
* Builds the HTTP GET method used to fetch the metadata. The returned method advertises support for GZIP and
* deflate compression, enables conditional GETs if the cached metadata came with either an ETag or Last-Modified
* information, and sets up basic authentication if such is configured.
*
* @return the constructed GET method
*/
protected GetMethod buildGetMethod() {
GetMethod getMethod = new GetMethod(getMetadataURI());
getMethod.addRequestHeader("Connection", "close");
getMethod.setRequestHeader("Accept-Encoding", "gzip,deflate");
if (cachedMetadataETag != null) {
getMethod.setRequestHeader("If-None-Match", cachedMetadataETag);
}
if (cachedMetadataLastModified != null) {
getMethod.setRequestHeader("If-Modified-Since", cachedMetadataLastModified);
}
if (httpClient.getState().getCredentials(authScope) != null) {
log.debug("Using BASIC authentication when retrieving metadata from '{}", metadataURI);
getMethod.setDoAuthentication(true);
}
return getMethod;
}
示例3: assertAuthenticatedHttpStatus
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
/** Verify that given URL returns expectedStatusCode
* @throws IOException */
public void assertAuthenticatedHttpStatus(Credentials creds, String urlString, int expectedStatusCode, String assertMessage) throws IOException {
URL baseUrl = new URL(HTTP_BASE_URL);
AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
GetMethod getMethod = new GetMethod(urlString);
getMethod.setDoAuthentication(true);
Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
try {
httpClient.getState().setCredentials(authScope, creds);
final int status = httpClient.executeMethod(getMethod);
if(assertMessage == null) {
assertEquals(urlString,expectedStatusCode, status);
} else {
assertEquals(assertMessage, expectedStatusCode, status);
}
} finally {
httpClient.getState().setCredentials(authScope, oldCredentials);
}
}
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:22,代码来源:AuthenticatedTestUtil.java
示例4: doDemo
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
private void doDemo() throws IOException {
HttpClient client = new HttpClient();
client.getParams().setParameter(
CredentialsProvider.PROVIDER, new ConsoleAuthPrompter());
GetMethod httpget = new GetMethod("http://target-host/requires-auth.html");
httpget.setDoAuthentication(true);
try {
// execute the GET
int status = client.executeMethod(httpget);
// print the status and response
System.out.println(httpget.getStatusLine().toString());
System.out.println(httpget.getResponseBodyAsString());
} finally {
// release any connection resources used by the method
httpget.releaseConnection();
}
}
示例5: main
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
// pass our credentials to HttpClient, they will only be used for
// authenticating to servers with realm "realm" on the host
// "www.verisign.com", to authenticate against
// an arbitrary realm or host change the appropriate argument to null.
client.getState().setCredentials(
new AuthScope("www.verisign.com", 443, "realm"),
new UsernamePasswordCredentials("username", "password")
);
// create a GET method that reads a file over HTTPS, we're assuming
// that this file requires basic authentication using the realm above.
GetMethod get = new GetMethod("https://www.verisign.com/products/index.html");
// Tell the GET method to automatically handle authentication. The
// method will use any appropriate credentials to handle basic
// authentication requests. Setting this value to false will cause
// any request for authentication to return with a status of 401.
// It will then be up to the client to handle the authentication.
get.setDoAuthentication( true );
try {
// execute the GET
int status = client.executeMethod( get );
// print the status and response
System.out.println(status + "\n" + get.getResponseBodyAsString());
} finally {
// release any connection resources used by the method
get.releaseConnection();
}
}
示例6: getAuthenticatedContent
import org.apache.commons.httpclient.methods.GetMethod; //导入方法依赖的package包/类
/** retrieve the contents of given URL and assert its content type
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @throws IOException
* @throws HttpException */
public String getAuthenticatedContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
final GetMethod get = new GetMethod(url);
URL baseUrl = new URL(HTTP_BASE_URL);
AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
get.setDoAuthentication(true);
Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
try {
httpClient.getState().setCredentials(authScope, creds);
if(params != null) {
final NameValuePair [] nvp = new NameValuePair[0];
get.setQueryString(params.toArray(nvp));
}
final int status = httpClient.executeMethod(get);
final InputStream is = get.getResponseBodyAsStream();
final StringBuffer content = new StringBuffer();
final String charset = get.getResponseCharSet();
final byte [] buffer = new byte[16384];
int n = 0;
while( (n = is.read(buffer, 0, buffer.length)) > 0) {
content.append(new String(buffer, 0, n, charset));
}
assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")",
expectedStatusCode,status);
final Header h = get.getResponseHeader("Content-Type");
if(expectedContentType == null) {
if(h!=null) {
fail("Expected null Content-Type, got " + h.getValue());
}
} else if(CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
// no check
} else if(h==null) {
fail(
"Expected Content-Type that starts with '" + expectedContentType
+" but got no Content-Type header at " + url
);
} else {
assertTrue(
"Expected Content-Type that starts with '" + expectedContentType
+ "' for " + url + ", got '" + h.getValue() + "'",
h.getValue().startsWith(expectedContentType)
);
}
return content.toString();
} finally {
httpClient.getState().setCredentials(authScope, oldCredentials);
}
}
开发者ID:apache,项目名称:sling-org-apache-sling-launchpad-integration-tests,代码行数:55,代码来源:AuthenticatedTestUtil.java