1 Replies - 5578 Views - Last Post: 30 September 2015 - 03:23 PM Rate Topic: -----

#1 supdog   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-May 15

how to make cURL requests in java?

Posted 30 September 2015 - 03:00 PM

Hello everyone.

I am trying to make a cURL request to a website called oanda. I am trying to run the cURL request threw the command line with java code. The response will be in JSON. Code oanda gives you as an example of a cURL request goes as follows:
curl -i "https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD"


I have downloaded the cURL software and saved the curl.exe program in my C drive. I have written this java code to try to test the cURL request in the command line:

import java.io.*;

public class FxTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
        		 "cmd.exe", "/c", "cd \"C:\\curl https://www.youtube.com/");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}



When I run this I get this error:
The filename, directory name, or volume label syntax is incorrect.


I have also written java code for the command that oanda provides as a request:

import java.io.*;

public class FxTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
        		 "cmd.exe", "/c", "cd \"C:\\curl -i \"https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD\"");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}



When I run this I get the same error. If you could explain how this code is wrong and provide a link or example explaining how to fix it that would be awesome! Thank you for your Help!!!

Is This A Good Question/Topic? 0
  • +

Replies To: how to make cURL requests in java?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: how to make cURL requests in java?

Posted 30 September 2015 - 03:23 PM

The command makes no sense. cd means 'change directory' and the quoting looks wrong.

In any case, why use an external program to make http requests when Java supports that?

This post has been edited by g00se: 30 September 2015 - 03:24 PM
Reason for edit:: Clarification

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1