0

I've found there are several discussions about this topic, but what confuses me is another question. You see, the Object URL is used to get data from certain page. But how to write the URL when the page requires authorities?

The API suggests the curl code curl -i -X GET --header 'X-Auth-code:<your_code>' to be used, but how?

Behzad
  • 3,502
  • 4
  • 36
  • 63
Stone
  • 43
  • 1
  • 8
  • By authorities, I suppose you mean the custom `X-Auth-code` header? If that's so, you can add custom headers to GET request in java. Just google or search on StackOverflow. Btw, just because the API suggest usage of curl, doesn't mean you have to use curl only. You can use standard JDK components / open source libraries to make HTTP requests. – Niks Feb 28 '16 at 06:01

3 Answers3

2

To run command in a Java program, you could use Process and Runtime.

Try something like bellow:

Process p = Runtime.getRuntime().exec("curl -i -X ");
InputStream is = p.getInputStream();
mmuzahid
  • 2,252
  • 23
  • 42
  • 1
    What is the problem? – mmuzahid Feb 28 '16 at 06:10
  • 1
    Could be a problem with quoting. Or more specifically that `exec` does not understand quoting at all. The solution is to supply the command name and options as an array of strings ... so that you don't need any quoting. – Stephen C Feb 28 '16 at 06:33
  • 1
    @Stone - you need to show use >>exactly<< what you tried. – Stephen C Feb 28 '16 at 06:37
  • @mmuzahid sorry for your waiting, I just found the api gave me a wrong curl command. After correcting it, there only remains the question that I use url to link the Api – Stone Feb 28 '16 at 06:52
  • But error showed that MalformedURLException: no protocol. I am running on Windows, is there any chance that the Windows does not support curl? – Stone Feb 28 '16 at 06:54
  • @Stone - may be an encode problem you have. Try something like ``java.net.URLEncoder.encode(paramValue, "UTF-8");`` before using it – mmuzahid Feb 28 '16 at 06:57
  • Why are you not updating your question with the code you have tried? – mmuzahid Feb 28 '16 at 07:00
  • @mmuzahid you know I am now in mainland China so I can hardly visit stackoverflow site using pc. But I just fixed the problem and can post the code, so please have a look. Many thanks – Stone Feb 28 '16 at 07:18
  • On one occasion I tried String requestUrl = "curl -X GET --header \"X-Auth-Code: d16e16f5ba658c1118f52666b1469d96\" -i http://121.41.106.89:8010/"; and use URL url = new URL(requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection)url.openConnection(); – Stone Feb 28 '16 at 07:24
  • this gives the error: java.net.MalformedURLException: no protocol: curl -X GET --header "X-Auth-Code: d16e16f5ba658c1118f52666b1469d96" -i http://121.41.106.89:8010/ – Stone Feb 28 '16 at 07:24
  • I also try to use process, but it has little connection with my intention, that is to get the json type data from the site http://121.41.106.89:8010/ – Stone Feb 28 '16 at 07:27
1

This is the final solution: the key is the setRequestProperty() method

     try {
        String url = "http://121.41.106.89:8010/";

        URL readUrl = new URL(url);
        URLConnection connection = readUrl.openConnection();
        connection.setConnectTimeout(5000);
        connection.setRequestProperty("X-Auth-Code", "75d07493b655591137dbc905ede428ce");
        connection.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String result = in.readLine();
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
Stone
  • 43
  • 1
  • 8
0
    URL url = new URL("http://stackoverflow.com");

    try (BufferedReader reader = new BufferedReader(new   InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }

I think this reference may help you.

Community
  • 1
  • 1
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19
  • Yep, I know your idea. But what is important is when authority is required. Your suggestion does not solve this problem, I 'm afraid – Stone Feb 28 '16 at 05:43
  • you can set the user and password in header of post request. check this Url als : http://stackoverflow.com/questions/3283234/http-basic-authentication-in-java-using-httpclient. @Stone – Saman Salehi Feb 29 '16 at 06:33