🔨
Java Utility Package
🔨
Java Utility Package
  • Java Utility Package (Open Source)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • 🔑Password Vault
    • 🖥️HTTP / Socket Server
    • ⚒️HTTP Client
    • 🖥️Socket Client
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
    • 💡Tips / FAQ
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Sample Code
    • 📖JavaDoc API
    • 👨‍💻GitHub
Powered by GitBook
  1. Examples

HTTP Client

Get data from server the easy way.

Last updated 1 month ago

KHTTPClient Class Overview

  • Comprehensive HTTP Method Support Fully supports all major HTTP methods, including GET, POST, PUT, PATCH, HEAD, OPTIONS, and DELETE.

  • Automatic Header Management Automatically adds essential request headers, such as Date, User-Agent, Host, and Content-Length, ensuring seamless communication with HTTP servers.

For servers requiring OAuth 2.0 authentication, leverage the (Freeware) to obtain access tokens efficiently and securely.

Example GET Request

KHTTPClient http = new KHTTPClient();

if (!http.get("https://reqbin.com/echo/get/json")) {
   KLog.error("Error: {}", http.getLastError());
} else {
   System.out.println(http.getResponseDataAsString());
}

Example POST Request with Basic Authentication

Properties props = new Properties();
props.put("Authorization", "Basic " + K.encodeBase64(userName + ':' + password);

KHTTPClient http = new KHTTPClient();

if (!http.post("https://example.com:4443", props, "HTTP body data")) {
   KLog.abort("HTTP POST failed - " + http.getLastError());
}
Simple OAuth 2.0 Framework for Authentication
⚒️
Page cover image