액세스 할 사용자 이름/암호 (html 양식 기반 인증)가 필요한 페이지에서 zip 파일을 다운로드하려고했습니다. 내가 아파치 http 라이브러리를 사용하고 있습니다. 이전에 필자는 비슷한 것을 만들었습니다. 그 페이지에는 파일을 다운로드하기위한 암호 만 필요했습니다. 당신이 양식이 이름 이메일과 비밀번호로 두 개의 매개 변수를 가지고 찾을 수 코드에서 제공하는 URL을 열면 여기에 내 코드apache를 사용하여 파일을 다운로드하십시오. http
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public URI lastRedirectedUri;
public boolean isRedirected(HttpRequest request,
HttpResponse response,
HttpContext context) {
boolean isRedirect = false;
try {
isRedirect =
super.isRedirected(request, response,
context);
} catch (org.apache.http.ProtocolException e) {
e.printStackTrace();
}
if (!isRedirect) {
int responseCode =
response.getStatusLine().getStatusCode();
if (responseCode == 301 ||
responseCode == 302) {
System.out.println("the original response code is" + responseCode);
return true;
}
}
return isRedirect;
}
// public URI getLocationURI(HttpResponse response, HttpContext context)
// throws ProtocolException {
//
// lastRedirectedUri = super.getLocationURI(request , response, context);
//
// return lastRedirectedUri;
// }
});
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
// formparams.add(new BasicNameValuePair("password", arg[1]));
formparams.add(new BasicNameValuePair("password", "*****"));
formparams.add(new BasicNameValuePair("email", "****"));
UrlEncodedFormEntity entity1 =
new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost =
new HttpPost("https://*************************/l/?next=/s/48750/d/");
// new HttpPost(arg[0]);
httppost.setEntity(entity1);
HttpContext localContext = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
localContext.setAttribute(ClientContextConfigurer.COOKIE_STORE, cookieStore);
HttpResponse response = httpclient.execute(httppost, localContext);
HttpHost target =
(HttpHost)localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
System.out.println("Final target: " + target);
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if (entity != null) {
FileOutputStream fos =
new java.io.FileOutputStream("download.zip");
entity.writeTo(fos);
fos.close();
}
이며, formparams (값은 위의 코드에 주석으로 내가 그들을 공급하고 있습니다). 도움이 될 것입니다.
귀하의 코드를 좋아 보인다, 당신은 어떤 오류가있다? –
나는 출력 스트림을 쓰고있는 "download.zip"파일을 체크 할 때 어떤 에로스도 얻지 못한다. 예상하지 못한 4kb 파일이다. 이 zip 파일의 확장자를 html로 변경하고 메모장에서 열었습니다. 로그인 페이지와 동일한 페이지 (전자 메일 및 암호 필드 포함)입니다. 정확히 동일하지는 않습니다. – Rpant