Christian Amani
Christian Amani

Reputation: 370

How to make a https post in Java?

Hello being a developer beginning in the world of Java I would want to know how to write the equivalent of this curl in Java.

curl -x POST \ -H "Authorization:Basic Sisi" \ -d "grant_type=client_credentials" \ "https://api.orange.com/oauth/v2/token"

Upvotes: 0

Views: 1358

Answers (1)

Atul Kumar
Atul Kumar

Reputation: 419

You can use Apache HttpClient to implement this in java.

One example is -

String url = "https://api.orange.com/oauth/v2/token";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Basic Sisi");
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
//Print result

Read more here - http://www.mkyong.com/java/apache-httpclient-examples/

Upvotes: 2

Related Questions