[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;
}
}