session - Dynamic Webpage download in Java using ASP.NET_SessionId Cookie -


the below code fetches asp.net_sessionid cookie www.jazzcinemas.com , opens connection dynamic webpage of same domain using cookie obtained using addrequestproperty("cookie",cookie) method , tried download using download() method. download file contains home page not dynamic page wanted. please help.

 import java.net.url;  import javax.net.ssl.httpsurlconnection;  import java.io.*;  import java.net.*;  import java.util.*;  import java.security.cert.certificate;  import javax.net.ssl.sslpeerunverifiedexception;   class main{  public static void main(string args[]){     string address = "https://www.jazzcinemas.com";     string s = "https://www.jazzcinemas.com/chennai/movie/neruppuda";     new main().test(address,s);  }  // method open https connection void test(string address,string s){      try{             url link = new url(address);             url link1 = new url(s);              httpsurlconnection my_connection = (httpsurlconnection) link.openconnection();             print_cert_info(my_connection);     // method print  ssl certificate information 

below line stores cookies , disconnects first connection.

            list<string> cookies = my_connection.getheaderfields().get("set-cookie");             for(string a:cookies) system.out.println("cookies: "+a);             my_connection.disconnect(); 

now opening connection dynamic web page , using cookie got before. response code 200 indicating connection successful.

            httpsurlconnection my_connection1 = (httpsurlconnection) link1.openconnection();                   my_connection1.setrequestproperty("user-agent", "mozilla/5.0 (windows nt 6.1; wow64; rv:25.0) applewebkit/537.36 (khtml, gecko) chrome/51.0.2704.63 safari/537.36");                 my_connection1.addrequestproperty("cookie",cookies.get(0).split(";")[0]);                   my_connection1.addrequestproperty("cookie",cookies.get(0).split(";")[1]);                 my_connection1.addrequestproperty("cookie",cookies.get(0).split(";")[2]);                 my_connection1.setrequestmethod("get");                 my_connection1.setusecaches(false);                 my_connection1.setrequestproperty("accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");                 my_connection1.setrequestproperty("accept-language", "en-us,en;q=0.5");                  system.out.println(my_connection1.getresponsecode());                 download(my_connection1);        }catch(malformedurlexception e){         e.printstacktrace();     }catch(ioexception e){         e.printstacktrace();     }  }  // method certificates , print void print_cert_info(httpsurlconnection my_connection){     if(my_connection != null){         try{             system.out.println("response code: "+ my_connection.getresponsecode());             system.out.println("cipher suite: "+my_connection.getciphersuite());             system.out.println();             certificate[] certs = my_connection.getservercertificates();             for(certificate cert :certs){                 system.out.println("cert type: " + cert.gettype());                 system.out.println("cert hash code: "+cert.hashcode());                 system.out.println("cert public key algorithm: "+cert.getpublickey().getalgorithm());                 system.out.println("cert public key format: "+cert.getpublickey().getformat());                 system.out.println();              }         }catch(sslpeerunverifiedexception e){             e.printstacktrace();         }catch(ioexception e){             e.printstacktrace();         }     }  } 

below download method using..

void download(httpsurlconnection u){ try {   inputstream in = u.getinputstream(); bytearrayoutputstream out = new bytearrayoutputstream(); byte[] buf = new byte[1024]; int n = 0;  while (-1!=(n=in.read(buf))){     out.write(buf, 0, n); }  out.close(); in.close();  byte[] response = out.tobytearray();   fileoutputstream fos = new fileoutputstream("c://users/nikki/neruppuda"+".html");  fos.write(response); fos.close(); } catch(ioexception e){     system.out.println(e); }  } } 


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -