자바 에서 httpclient 의 digest 인증

3702 단어
package test.util;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * @author Administrator
 * 
 */
public class HTTPDigestClient {
	private URI serverURI = null;
	String response = null;
	private DefaultHttpClient httpClient = new DefaultHttpClient();

	/**
	 * constructor
	 */
	public HTTPDigestClient(String userName, String passWord, String url) {
		try {
			serverURI = new URI(url);
			Credentials creds = new UsernamePasswordCredentials(userName,
					passWord);

			httpClient.getCredentialsProvider().setCredentials(
					new AuthScope(serverURI.getHost(), serverURI.getPort), (Credentials) creds);

			httpClient.getParams().setParameter(
					AuthPolicy.AUTH_SCHEME_PRIORITY, Collections.singleton(AuthPolicy.DIGEST));
			httpClient.getAuthSchemes().register(AuthPolicy.DIGEST,
					new DigestSchemeFactory());

		} catch (Exception e) {
			
		}
	}

	/**
	 * send request to server
	 * 
	 * @param httpClient
	 * @param httpUriRequest
	 * @return response HttpResponse
	 */
	public HttpResponse send(HttpUriRequest httpUriRequest) {
		HttpResponse response = null;
		try {
			if (null == httpClient) {
				
				return response;
			}
			response = httpClient.execute(httpUriRequest);

		} catch (Exception e) {
			
		}
		return response;
	}

}

 
서버 쪽 도 메 인 가입 해 야 합 니 다.
realm 이 비어 있 거나 현재 도 메 인 이름 으로 설정 테스트 성공
사용자 이름 비밀 번 호 는 도 메 인 계 정 을 사용 합 니 다.
다음은 basic 검증 방식 입 니 다. 테스트 통과.
 
package test.util;

import java.net.URI;
import java.util.Collections;

import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * @author Administrator
 * 
 */
public class HTTPBasicClient {

	private URI serverURI = null;
	String response = null;
	private DefaultHttpClient httpClient = new DefaultHttpClient();

	/**
	 * constructor
	 * 
	 */
	public HTTPBasicClient(String userName, String passWord, String url) {
		try {
			serverURI = new URI(url);

			UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
					userName, passWord);
			CredentialsProvider credsProvider = httpClient
					.getCredentialsProvider();
			credsProvider.setCredentials(new AuthScope(serverURI.getHost(),
					serverURI.getPort()), (Credentials) creds);
			httpClient.getParams().setParameter(
					AuthPolicy.AUTH_SCHEME_PRIORITY,
					Collections.singleton(AuthPolicy.BASIC));
			httpClient.getAuthSchemes().register(AuthPolicy.BASIC,
					new BasicSchemeFactory());

		} catch (Exception e) {
			
		}
	}

	/**
	 * send request to server
	 * 
	 * @param httpClient
	 * @param httpUriRequest
	 * @return response HttpResponse
	 */
	public HttpResponse send(HttpUriRequest httpUriRequest) {
		HttpResponse response = null;
		try {
			if (null == httpClient) {
				
				return response;
			}
			response = httpClient.execute(httpUriRequest);

		} catch (Exception e) {
			
		}
		return response;
	}

}

좋은 웹페이지 즐겨찾기