3

I'm a newbie in java and need help in how to use the cURL in java and saving the output of the cURL command to a file in runtime.

Let's consider as i'm using the below URL which i have to cURL in the linux machine.

http://maniv.com/maniv/rest?method=sendMessage&msg_type=binary&parameter1=9999999&parameter2=9999999

When i'm hitting the above URL using curl "http://maniv.com/maniv/rest?method=sendMessage&msg_type=binary&parameter1=9999999&parameter2=9999999" in linux, it will give the ouput as:

Message | sent | successfully

Now i need to write the output into a new file based on the parameter1 as file name each time, when i change the parameter1 and hit the URL.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
dm90
  • 775
  • 1
  • 10
  • 24

2 Answers2

1

Do you have to use curl command with Java? Could you use HttpClient to get html file? http://hc.apache.org/httpclient-3.x/

Please refer to this question: Read url to string in few lines of java code. It provides pretty good answer.

Community
  • 1
  • 1
longhua
  • 4,142
  • 21
  • 28
  • Anything is fine.. I just need to code this. Since, i'm a newbie i just need idea for `HTTP` or `cURL` in java. – dm90 Mar 06 '13 at 09:43
  • This link maybe a proper place for you: http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html. All you need is to read html file from a URL. – longhua Mar 06 '13 at 09:46
  • Dude, mine is not a static URL. `http://maniv.com/maniv/rest?method=sendMessage&msg_type=binary&**parameter1=9999999**&**parameter2=9999999**`. The **PARAMETERT1** and **PARAMETER2** are the user inputs. So, in `URL oracle = new URL("http://www.oracle.com/");` i could not use. – dm90 Mar 06 '13 at 09:51
  • It seems that you are trying to make a POST operation: http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – longhua Mar 06 '13 at 09:56
1

Thanks for the effort. I got the answer after a long try! `

    String url = "http://maniv.com/maniv/rest";
    String charset = "UTF-8";
    String method = "sendMessage";
    String msg_type = "binary";
    String parameter1 = "9999999";
    String parameter2 = "0000000";
    String msg = "001100110001100011";

    // ...
    StringBuffer sb = null;
    String query = String
            .format("method=%s&msg_type=%s&userid=%s&password=%s&msg=%s",
                    URLEncoder.encode(method, charset),
                    URLEncoder.encode(msg_type, charset),
                    URLEncoder.encode(userid, charset),
                    URLEncoder.encode(password, charset),
                    URLEncoder.encode(msg, charset));
    try {
        URL requestUrl = new URL(url + "?" + query);
        HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");
        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
        osw.write(query);
        osw.flush();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String in = "";
        sb = new StringBuffer();
        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
            System.out.println("Output:\n" +in);
        }
    } catch (Exception e) {
        System.out.println("Exception occured" + e);
    }
}

}`

dm90
  • 775
  • 1
  • 10
  • 24