android sdk 4.2.2,在Android SDK(使用4.2.2)HttpClient基本抢先认证与HttpPost总是返回401...

使用的Webservice是:https://domain.name.com/EWS/Exchange.asmx,在soapUI中完美工作。对于AuthScope,也试过new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),但没有运气。在Android SDK(使用4.2.2)HttpClient基本抢先认证与HttpPost总是返回401

下面的代码是基于ClientPreemptiveBasicAuthentication.java的版本4.0.3中的Apache示例代码。在http://archive.apache.org/dist/httpcomponents/httpclient/source/

private void callBasicAuthencationWebService() {

DefaultHttpClient httpclient = new DefaultHttpClient();

Credentials creds = new UsernamePasswordCredentials(username, password);

httpclient.getCredentialsProvider().setCredentials(

new AuthScope("domain.name.com", 443),

creds);

BasicHttpContext localcontext = new BasicHttpContext();

// Generate BASIC scheme object and stick it to the local

// execution context

BasicScheme basicAuth = new BasicScheme();

localcontext.setAttribute("preemptive-auth", basicAuth);

HttpHost targetHost = new HttpHost("domain.name.com", 443, "https");

HttpPost httppost = new HttpPost(urlString);

// add headers

httppost.setHeader("soapaction", soapAction);

httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

try {

httppost.setEntity(new StringEntity(envelope));

//httppost.addHeader(BasicScheme.authenticate(creds,"US-ASCII",false));

Header bs = new BasicScheme().authenticate(creds, httppost);

httppost.addHeader("Proxy-Authorization", bs.getValue());

}

catch (Exception e) {

System.out.println(e.toString());

}

// Add as the first request interceptor

httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);

System.out.println("executing request: " + httppost.getRequestLine());

System.out.println("to target: " + targetHost);

for (int i = 0; i < 3; i++) {

try {

HttpResponse response = httpclient.execute(targetHost, httppost, localcontext);

HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");

StatusLine statusLine = response.getStatusLine();

int statusCode = statusLine.getStatusCode();

String statusMessage = statusLine.getReasonPhrase();

System.out.println("Status Code: " + statusCode + statusMessage);

if (entity != null) {

System.out.println("Response content length: " + entity.getContentLength());

entity.consumeContent();

}

} catch (Exception e) {

System.out.println(e.toString());

}

}

// When HttpClient instance is no longer needed,

// shut down the connection manager to ensure

// immediate deallocation of all system resources

httpclient.getConnectionManager().shutdown();

}

PreemptiveAuth内部类,使用HttpRequestInterceptor:

static class PreemptiveAuth implements HttpRequestInterceptor {

public void process(

final HttpRequest request,

final HttpContext context) throws HttpException, IOException {

AuthState authState = (AuthState) context.getAttribute(

ClientContext.TARGET_AUTH_STATE);

// If no auth scheme avaialble yet, try to initialize it preemptively

if (authState.getAuthScheme() == null) {

AuthScheme authScheme = (AuthScheme) context.getAttribute(

"preemptive-auth");

CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(

ClientContext.CREDS_PROVIDER);

HttpHost targetHost = (HttpHost) context.getAttribute(

ExecutionContext.HTTP_TARGET_HOST);

if (authScheme != null) {

Credentials creds = credsProvider.getCredentials(

new AuthScope(

targetHost.getHostName(),

targetHost.getPort()));

if (creds == null) {

throw new HttpException("No credentials for preemptive authentication");

}

authState.setAuthScheme(authScheme);

authState.setCredentials(creds);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值