HttpClient的4,302错误怎么重定向? [英] Httpclient 4, error 302. How to redirect?
问题描述
我要访问的第一需要(Tomcat服务器)身份验证一个网站,然后用POST请求登录并保持用户看到网站的网页。我使用的HttpClient 4.0.1
第一认证工作正常,但不能登录总是抱怨这个错误:302临时移动
我把饼干和放大器;我把一个上下文,但一无所获。事实上,似乎登录的作品,因为如果我写不正确的参数或用户密码||,我看到登录页面。所以我猜什么行不通是自动重定向。
随着我的code,它总是抛出IOException异常,302:
DefaultHttpClient的HttpClient =新DefaultHttpClient();
的CookieStore的CookieStore =新BasicCookieStore();
httpclient.getParams()的setParameter(
ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
HttpContext的背景下=新BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE,的CookieStore);
// ResponseHandler所<串GT; ResponseHandler所=新BasicResponseHandler(); 凭据testsystemCreds =新UsernamePasswordCredentials(TESTSYSTEM_USER,TESTSYSTEM_PASS);
httpclient.getCredentialsProvider()。setCredentials方法(
新AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT)
testsystemCreds); HttpPost postRequest =新HttpPost(CMS +/登录);
清单<&的NameValuePair GT; formparams =新的ArrayList<&的NameValuePair GT;();
formparams.add(新BasicNameValuePair(pUserId用户));
formparams.add(新BasicNameValuePair(pPassword,通));
postRequest.setEntity(新UrlEn codedFormEntity(formparams,UTF-8));
HTT presponse响应= httpclient.execute(postRequest,背景);
的System.out.println(响应); 如果(response.getStatusLine()的getStatus code()!= HttpStatus.SC_OK)
抛出新IOException异常(response.getStatusLine()的toString()); HttpUriRequest currentReq =(HttpUriRequest)context.getAttribute(
ExecutionContext.HTTP_REQUEST);
HttpHost currentHost =(HttpHost)context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
串CURRENTURL = currentHost.toURI()+ currentReq.getURI();
的System.out.println(CURRENTURL); HttpEntity实体= response.getEntity();
如果(实体!= NULL){
长LEN = entity.getContentLength();
如果(LEN = -1&安培;!&放大器; LEN< 2048){
的System.out.println(EntityUtils.toString(实体));
}其他{
//流出来的内容
}
}
有关4.1版:
DefaultHttpClient的HttpClient =新DefaultHttpClient();
httpclient.setRedirectStrategy(新DefaultRedirectStrategy(){
公共布尔isRedirected(HTT prequest请求的Htt presponse响应,HttpContext的背景下){
布尔isRedirect = FALSE;
尝试{
isRedirect = super.isRedirected(请求,响应上下文);
}赶上(的ProtocolException E){
// TODO自动生成catch块
e.printStackTrace();
}
如果(!isRedirect){
。INT响应code = response.getStatusLine()的getStatus code();
如果(响应code == || 301响应code == 302){
返回true;
}
}
返回isRedirect;
}
});
I want to access one site that first requires an (tomcat server) authentication and then log in with a POST request and keep that user to see the site's pages. I use Httpclient 4.0.1
The first authentication works fine but not the logon that always complains about this error: "302 Moved Temporarily"
I keep cookies & I keep a context and yet nothing. Actually, it seems that the logon works, because if I write incorrect parameters or user||password, I see the login page. So I guess what doesn't work is the automatic redirection.
Following my code, which always throws the IOException, 302:
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
httpclient.getParams().setParameter(
ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//ResponseHandler<String> responseHandler = new BasicResponseHandler();
Credentials testsystemCreds = new UsernamePasswordCredentials(TESTSYSTEM_USER, TESTSYSTEM_PASS);
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
testsystemCreds);
HttpPost postRequest = new HttpPost(cms + "/login");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("pUserId", user));
formparams.add(new BasicNameValuePair("pPassword", pass));
postRequest.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
HttpResponse response = httpclient.execute(postRequest, context);
System.out.println(response);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
throw new IOException(response.getStatusLine().toString());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
HttpHost currentHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
String currentUrl = currentHost.toURI() + currentReq.getURI();
System.out.println(currentUrl);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 2048) {
System.out.println(EntityUtils.toString(entity));
} else {
// Stream content out
}
}
For 4.1 version:
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect=false;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
});
这篇关于HttpClient的4,302错误怎么重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!