0

Please find below my Java script to run a curl command which has to fetch all the request headers.

public class Test 
{

public static void main(String[] args) throws ClientProtocolException, IOException 
{
    String url="https://www.url.com/";
    String[] command = {"curl", "-v", "--header", "Pragma: x-cache-on,x-cache-remote-on,x-check-cacheable,x-get-cache-key,x-get-extracted-values,x-get-nonces,x-get-ssl-client-session-id,x-get-true-cache-key,x-serial-no,-x-get-request-id", url};
    ProcessBuilder process = new ProcessBuilder(command); 
    Process p;
    try
    {
        p = process.start();
        BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
        System.out.println("reading: "+reader.readLine()); //Not getting any output
        StringBuilder builder = new StringBuilder();
        String line = null;
        while ( (line = reader.readLine()) != null)
        {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
        }
        String result = builder.toString();
        System.out.print("output: "+result); //No output
}
    catch (Exception e)
    {
        System.out.print("error"); //No exception
        e.printStackTrace();
    }

}

}

When I run the above mentioned curl command in the command prompt I am able to get the correct output.

Please help me in fetching the same output in Java script as well.

Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
  • 1- try to read error stream `p.getErrorStream()` 2- why don't you use `HttpURLConnection` in JAVA API to make that request? instead of executing a curl command? – Yazan Aug 29 '17 at 07:07
  • Can you please give me the code snippet using HttpURLConnection ? – user2552735 Aug 29 '17 at 07:09
  • just google it, you will find tons of results, for ex: https://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnection – Yazan Aug 29 '17 at 07:47

0 Answers0