Skip to content

A library to make it easy to send HTTP requests in Java.

License

Notifications You must be signed in to change notification settings

BoxResin/JavaHTTP

Repository files navigation

NOTE: This repository has been deprecated. Use OkHttp.

Download GitHub license

java-logo _http_logo_dpwnload 2

About

A light-weight HTTP client library for Java. It's based on HttpURLConnection in java network package.

light-weight library

Getting Started

Add the following to app's build.gradle file:

dependencies { 
    implementation 'boxresin.library:JavaHTTP:1.1.1'
}

Usage

To send an HTTP request to google.com with GET method, do like below.

HttpResponse response = new HttpRequester()
    .setUrl("https://www.google.com/")
    .setMethod("GET")
    .request();

You can get information of the HTTP response from the HttpResponse object.

int statusCode = response.getStatusCode();
String statusMessage = response.getStatusMessage();
String body = response.getBody();
String headerValue = response.getHeader("header-key");

NOTE: To use this library in Android project, you have to add INTERNET permission to AndroidManifest.xml first. Furthermore, DO NOT call HttpRequester's request method on the UI thread.

  • Sending request with POST method
HttpResponse response = new HttpRequester()
    .setUrl("https://www.google.com/")
    .setMethod("POST")
    .addParameter("key", "value")
    .addParameter("key", "value")
    .addParameter("key", "value")
    .addParameter("key", "value")
    .request();
  • Sending request with some headers
HttpResponse response = new HttpRequester()
    .setUrl("https://www.google.com/")
    .setMethod("GET")
    .addHeader("key", "value")
    .addHeader("key", "value")
    .addHeader("key", "value")
    .addHeader("key", "value")
    .request();
  • URL query string with percent-encoding
// These keys and values will be percent encoded.
Map<String, String> params = new HashMap<>();
params.put("q", "java http 특수문자");
params.put("from", "https://github.com/BoxResin/JavaHTTP");

HttpResponse response = new HttpRequester()
        .setUrl("https://www.google.com/", params)
        .setMethod("GET")
        .request();

Document

You can see all documents of the library at here.

License

MIT License

Copyright (c) 2017 Minsuk Eom

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.