java http digest 认证_java中httpclient实现digest验证的请求

本文介绍了如何在Java中使用Apache HttpClient库实现HTTP Digest认证。通过HTTPDigestClient类,展示了如何构造客户端并发送请求,实现对服务器的身份验证。
摘要由CSDN通过智能技术生成

[java] view plain

copy

print ?

packagetest.util;

importjava.net.URI;

importjava.util.ArrayList;

importjava.util.List;

importorg.apache.commons.httpclient.auth.AuthPolicy;

importorg.apache.http.HttpResponse;

importorg.apache.http.auth.AuthScope;

importorg.apache.http.auth.Credentials;

importorg.apache.http.auth.UsernamePasswordCredentials;

importorg.apache.http.client.methods.HttpUriRequest;

importorg.apache.http.impl.auth.DigestSchemeFactory;

importorg.apache.http.impl.client.DefaultHttpClient;

/**

* @author Administrator

*

*/

publicclassHTTPDigestClient {

privateURI serverURI =null;

String response = null;

privateDefaultHttpClient httpClient =newDefaultHttpClient();

/**

* constructor

*/

publicHTTPDigestClient(String userName, String passWord, String url) {

try{

serverURI = newURI(url);

Credentials creds = newUsernamePasswordCredentials(userName,

passWord);

httpClient.getCredentialsProvider().setCredentials(

newAuthScope(serverURI.getHost(), serverURI.getPort), (Credentials) creds);

httpClient.getParams().setParameter(

AuthPolicy.AUTH_SCHEME_PRIORITY, Collections.singleton(AuthPolicy.DIGEST));

httpClient.getAuthSchemes().register(AuthPolicy.DIGEST,

newDigestSchemeFactory());

} catch(Exception e) {

}

}

/**

* send request to server

*

* @param httpClient

* @param httpUriRequest

* @return response HttpResponse

*/

publicHttpResponse send(HttpUriRequest httpUriRequest) {

HttpResponse response = null;

try{

if(null== httpClient) {

returnresponse;

}

response = httpClient.execute(httpUriRequest);

} catch(Exception e) {

}

returnresponse;

}

}

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验证方式 ,测试通过

[java] view plain

copy

print ?

packagetest.util;

importjava.net.URI;

importjava.util.Collections;

importorg.apache.commons.httpclient.auth.AuthPolicy;

importorg.apache.http.HttpResponse;

importorg.apache.http.auth.AuthScope;

importorg.apache.http.auth.Credentials;

importorg.apache.http.auth.UsernamePasswordCredentials;

importorg.apache.http.client.CredentialsProvider;

importorg.apache.http.client.methods.HttpUriRequest;

importorg.apache.http.impl.auth.BasicSchemeFactory;

importorg.apache.http.impl.client.DefaultHttpClient;

/**

* @author Administrator

*

*/

publicclassHTTPBasicClient {

privateURI serverURI =null;

String response = null;

privateDefaultHttpClient httpClient =newDefaultHttpClient();

/**

* constructor

*

*/

publicHTTPBasicClient(String userName, String passWord, String url) {

try{

serverURI = newURI(url);

UsernamePasswordCredentials creds = newUsernamePasswordCredentials(

userName, passWord);

CredentialsProvider credsProvider = httpClient

.getCredentialsProvider();

credsProvider.setCredentials(newAuthScope(serverURI.getHost(),

serverURI.getPort()), (Credentials) creds);

httpClient.getParams().setParameter(

AuthPolicy.AUTH_SCHEME_PRIORITY,

Collections.singleton(AuthPolicy.BASIC));

httpClient.getAuthSchemes().register(AuthPolicy.BASIC,

newBasicSchemeFactory());

} catch(Exception e) {

}

}

/**

* send request to server

*

* @param httpClient

* @param httpUriRequest

* @return response HttpResponse

*/

publicHttpResponse send(HttpUriRequest httpUriRequest) {

HttpResponse response = null;

try{

if(null== httpClient) {

returnresponse;

}

response = httpClient.execute(httpUriRequest);

} catch(Exception e) {

}

returnresponse;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值