Bir (tomcat sunucu) kimlik doğrulaması gerektiren ve daha sonra bir POST isteğiyle oturum açıp sitenin sayfalarını görmesini sağlayan bir siteye erişmek istiyorum. Kullandığım HttpClient 4.0.1Httpclient 4, 302 hatası. Nasıl yönlendirilir?
ilk kimlik doğrulama çalışıyor ama her zaman bu hata hakkında şikayet değil oturum açma:
Kurabiye & Bir bağlam ve henüz hiçbir şey tutmak tutmak "302 geçici taşındı". Aslında, oturum açma işlevi göründüğü için yanlış parametre veya kullanıcı şifresi yazarsam giriş sayfasını görüyorum. Bu yüzden işe yaramazsa otomatik yönlendirme olduğunu düşünüyorum. hep IOException atar kodumu, ardından, 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
}
}
"İlk kimlik doğrulama çalışıyor ama her zaman bu hata hakkında şikayet değil oturum açma". 302 yönlendirme, sunucu tarafından bir şikayet değildir; Bu, kullanıcı aracısının artık yanıtta belirtilen yeni sayfaya gitmesi gerektiğinin bir göstergesidir. –
Bunu düşündüm ama nasıl? O zaman bir GET isteği deneyin ama boşuna. – juanmirocks