N
N
Nikolay Baranenko2017-04-21 23:05:17
Java
Nikolay Baranenko, 2017-04-21 23:05:17

How to send an http request with a parameter similar to CURL?

Hello.
It is necessary to extend the capabilities of the HTTP client to java, which will initiate an HTTP request with a parameter
similar to

curl -H "Authorization: newParameter12" "http://server01/status"

how to add the ability to pass a parameter, similar to CURL, to the existing code?
String url = "http://server01/status";
        URL http_connect = null;
        try {
            http_connect = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(http_connect.openStream(), "UTF-8"))) {
            for (String line; (line = reader.readLine()) != null; ) {
                System.out.println(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Myvrenik, 2017-04-21
@drno-reg

This is called a header. Use HttpURLConnection :

HttpURLConnection сonnection = (HttpURLConnection) http_connect.openConnection();
сonnection.setRequestProperty("Authorization", "newParameter12");

And instead http_connect.openStream()use connection.getInputStream(). Only in real projects is it still worth using a ready-made solution, and not producing such bicycles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question