Solved GET, POST, and PUT (OAuth client) libraries

Discussion in 'Plugin Development' started by DoggyCode™, Jan 9, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    DoggyCode™

    I've worked with org.apache.httpcomponents's HttpClient before, but apparantly this library "is too big", and it's useless have I been told. So what do you suggest I use in order to make GET, POST, and PUT requests with headers, etc...?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @DoggyCode™ Normal HTTPConnections?
    Depends on what you want to do with them.
     
  3. Offline

    DoggyCode™

    All I want to be able to do is to send JSON Http GET, POST, and PUT requests which includes a header, and JSON-encoded dictionary as payload
     
  4. @DoggyCode™
    The standard java HttpURLConnection class can do all of that with ease.
    Code:java
    1. URL url = new URL("[URL]https://www.helloworld.com[/URL]"); // obviously without the "URL" thing, the forums are just screwing with it.
    2. HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    3. // Setting the content-type (all json services may not require this, but I know Mojang's do)
    4. httpConnection.setRequestProperty("content-type", "application/json");
    5. // Setting the request method
    6. httpConnection.setRequestMethod("POST");
    7. // Just tell the connection that we actually wan't to receive something back from this request.
    8. httpConnection.setDoOutput(true);
    9. // Method of writing to the connection, anything capable of writing to an output stream works.
    10. // Just be mindful to properly close the outputstream when you're done (either in a finally or try with resources block)
    11. OutputStreamWriter streamWriter = new OutputStreamWriter(httpConnection.getOutputStream());
    12. streamWriter.write("Hello World!");
    13. // Checking the response code, so we know no error has occurred.
    14. if (httpConnection.getResponseCode() == 200) {
    15. // Again, anything capable of reading from an inputstream will work here.
    16. // And as always, close properly.
    17. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
    18. System.out.println("Response: " + bufferedReader.readLine());
    19. }
     
    DoggyCode™ and timtower like this.
  5. Offline

    timtower Administrator Administrator Moderator

    @AlvinB @DoggyCode™ Don't forget to add a connection timeout though, that can be a real pain to deal with.
     
  6. timtower likes this.
  7. Offline

    DoggyCode™

    @AlvinB
    streamWriter.write("Hello World!");

    So basically instead of this, I would put my JSON object?
     
  8. Offline

    timtower Administrator Administrator Moderator

    Make sure to turn it into a string if not done already.
     
  9. Offline

    DoggyCode™

    Yeh, JsonObject#toString() am I right?
     
  10. Offline

    DoggyCode™

    Works perfectly. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page