4

I'm attempting to use a small Java program to make an HTTPs request (sending plain text of an SQL command) and trying to receive JSON data. Is this capability built into Java already or will I need external packages/libraries?

beefoak
  • 136
  • 1
  • 10
  • Sending plain SQL commands is not a good idea. That way, everyone can just modify or delete your database if they want – ByteHamster Aug 20 '18 at 17:07
  • I have the connection established using a token, so I'm the only one able to modify it as of right now. I just can't for the life of me figure out how to work the HTTP POST request – beefoak Aug 20 '18 at 17:11
  • Does this answer your question? [How to send HTTP request in java?](https://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java) – Mahozad Dec 31 '21 at 19:14

2 Answers2

3

https://square.github.io/okhttp/ is a good library for http interaction. Then you can use Jackson/Gson to parse the response to a typed object if you want

Usage

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

Maven:

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>retrofit</artifactId>
  <version>2.4.0</version>
</dependency>

or Gradle:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'

If you want to kill an ant with a Patton Tank, you have all sort of other options like spring, netflix OSS - feign client + ribbon etc

That being said, first, I would try out what Kayaman posted, https://www.baeldung.com/java-9-http-client as its one less dependency.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
1

There are multiple things you can do. When I normally need to use HTTP requests, I use the Jsoup parser, as it can send HTTP requests in a simple one-liner:

Jsoup.connect("google.com").data("key", "value").post();

You can get JSON responses with Jsoup by using ignoreContentType(true):

Jsoup.connect("https://postman-echo.com/post").data("derp1", "derp2").data("sql1", "sql2").ignoreContentType(true).post().body().html()

which results in this output:

{
   "args":{

   },
   "data":"",
   "files":{

   },
   "form":{
      "derp1":"derp2",
      "sql1":"sql2"
   },
   "headers":{
      "host":"postman-echo.com",
      "content-length":"21",
      "accept":"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
      "accept-encoding":"gzip",
      "content-type":"application/x-www-form-urlencoded; charset=UTF-8",
      "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36",
      "x-forwarded-port":"443",
      "x-forwarded-proto":"https"
   },
   "json":{
      "derp1":"derp2",
      "sql1":"sql2"
   },
   "url":"https://postman-echo.com/post"
}

From there, just parse the String as JSON using whatever JSON api you'd like (I personally use org.json and Gson). You can look more into the Jsoup API at the documentation located here.

If you'd like to refrain from utilizing APIs, this may be able to assist you better than I can, as it shows how to send a POST request utilizing both HTTPSUrlConnection, as well as Apache's HTTPClient.

Stephen
  • 376
  • 1
  • 3
  • 13