0

The purpose of this project is to check the redirections of a URL using curl command and then print the output. If the output is same as the provided URL (i.e. demo URL), then the program should give true value. The links to be checked are in the database table called "store" and the column containing the link values is called "link." I tried taking the link values from the database and adding it using the curl command but I get an internal error. It seems like the values are not being processed properly. I am also having difficulty with printing the output and checking to see if output is same as the "demo" URL. Any help would be greatly appreciated.

 public static void main(String args[]) {
            String link = null;
            String command1 = null;
             try
            {

              String myDriver = "com.mysql.cj.jdbc.Driver";
              String myUrl = "myurl";
              Class.forName(myDriver);
              Connection conn = DriverManager.getConnection(myUrl, "username", "password");
              String query = "SELECT * from store;";               
              Statement st = conn.createStatement();
              ResultSet rs = st.executeQuery(query);
              while (rs.next())
              {
                 link = rs.getString("link");
                 demolink = rs.getString("demo link");
                System.out.format("%s\n", link);
              }
              st.close();
            }
            catch (Exception e)
            {
              System.err.println("Got an exception! ");
              System.err.println(e.getMessage());
            }

//USING CURL COMMAND
            String host="host";
            String user="username";
            String password="password";
            System.out.println("generating CURL request");
            String command1="curl -v -L "+link+"";
            try{

                java.util.Properties config = new java.util.Properties(); 
                config.put("StrictHostKeyChecking", "no");
                JSch jsch = new JSch();
                Session session=jsch.getSession(user, host, 22);
                session.setPassword(password);
                session.setConfig(config);
                session.connect();
                System.out.println("Connected");

                Channel channel=session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command1);
                channel.setInputStream(null);
                ((ChannelExec)channel).setErrStream(System.err);

                InputStream in=channel.getInputStream();
                channel.connect();
                byte[] tmp=new byte[1024];
                while(true){
                  while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                   System.out.print(new String(tmp, 0, i));
                  }
                  if(channel.isClosed()){
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                  }
                  try{Thread.sleep(1000);}catch(Exception ee){}
                }
                channel.disconnect();

            }catch(Exception e){
                e.printStackTrace();
            }
Jdonut
  • 31
  • 5
  • 2
    Why so complicated? Java can open the URL and read the response without the need of curl. See e.g. https://stackoverflow.com/a/2793153/150978 – Robert Mar 28 '18 at 07:16
  • Also, no need to have the statement `Class.forName(myDriver);` at all. – Klitos Kyriacou Mar 28 '18 at 07:19
  • curl command is required for checking the redirections so i have to use this command :( – priya sharma Mar 28 '18 at 07:27
  • and i am not getting the link one by one in that command they are getting added in one whole string in the curl command .so how should i loop them so that it checks one link at a time and gives the output and then moves to another link. – priya sharma Mar 28 '18 at 07:29

0 Answers0