如何从httpclient获取身份验证方案?

时间:2012-03-13 06:48:51

标签: httpclient

我使用apache httpclient连接到url.Whenever我得到401错误我想知道服务器的身份验证方案? 像响应代码变得像response.getStatusLine()。getStatusCode()类似如何获得哪个auth方案?

1 个答案:

答案 0 :(得分:0)

HttpClient教程是你的朋友。


HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://localhost:8080/"); 
HttpResponse response = httpclient.execute(httpget, localContext);

AuthState targetAuthState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
System.out.println("Target auth scope: " + targetAuthState.getAuthScope());
System.out.println("Target auth scheme: " + targetAuthState.getAuthScheme());