无法移动到下一页http://indianvisa-bangladesh.nic.in/visa/Get_Appointment via httpClient 4.4
问题描述:
Hiii 更新2: 感谢4的帮助。 我现在得到了饼干,但我被困在同一页面上。 我添加了cookie作为标题与请求,但我得到了相同的页面作为响应。 不知道我做错了。 请指导我。无法移动到下一页http://indianvisa-bangladesh.nic.in/visa/Get_Appointment via httpClient 4.4
org.apache.http.impl.client.AbstractHttpClient httpclient = new org.apache.http.impl.client.DefaultHttpClient();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
System.out.println("Binding Cookie from Cookie Store="+this.cookies);
String c=this.cookies.replaceAll("Set-Cookie: ", "");
String c1=c.replaceAll(";path=/", "");
System.out.println("c="+c1);
String []ar=c1.split("=");
System.out.println("a[0]="+ar[0]);
System.out.println("a[1]="+ar[1]);
BasicClientCookie cookie = new BasicClientCookie(ar[0], ar[1]);
cookie.setDomain("indianvisa-bangladesh.nic.in");
cookie.setPath("/");
cookieStore.addCookie(cookie);
httpclient.setCookieStore(cookieStore);
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://indianvisa-bangladesh.nic.in/visa/Get_Appointment");
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("Page contect="+result);
// Consume response content
EntityUtils.consume(entity);
System.out.println("----------------------------------------");
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
//httpclient.getConnectionManager().shutdown();
}
答
尝试迭代httpclient.getCookieStore();
下面的例子http://www.programcreek.com/java-api-examples/index.php?api=org.apache.http.client.CookieStore
任何帮助将不胜感激!!!! – Rohit