Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/babyblue94520/url-request
Easy to use HttpUrlConnection
https://github.com/babyblue94520/url-request
http-client http-url-connection
Last synced: 13 days ago
JSON representation
Easy to use HttpUrlConnection
- Host: GitHub
- URL: https://github.com/babyblue94520/url-request
- Owner: babyblue94520
- License: apache-2.0
- Created: 2022-09-26T06:39:27.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-04T07:24:15.000Z (over 1 year ago)
- Last Synced: 2024-04-16T18:18:00.044Z (9 months ago)
- Topics: http-client, http-url-connection
- Language: Java
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Url Request
## Overview
單純覺得市面上的 Http Client 很難用,只好自己開發一個基於`java.net.HttpUrlConnection`,實現一個更容易使用的 `URLRequest`。
## QuickStart
### Usage
* Default return `string` body.
```java
import pers.clare.urlrequest.URLRequest;
import pers.clare.urlrequest.URLResponse;import java.util.concurrent.Executors;
class Example {
public static void main(String[] args) {
URLResponse response = URLRequest.build(url)
.header(HeaderNames.CONTENT_TYPE, HeaderValues.JSON)
.body(om.writeValueAsString(data))
.get(); // or post(), put() and delete()
String body = response.getBody();
}
}```
* Custom response handler.
```java
import pers.clare.urlrequest.URLRequest;
import pers.clare.urlrequest.URLResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import pers.clare.urlrequest.handler.ResponseHandler;import java.io.InputStreamReader;
import java.util.concurrent.Executors;class Example {
public static void main(String[] args) {
ObjectMapper om = new ObjectMapper();
ResponseHandler handler = (in, charset) -> om.readValue(new InputStreamReader(in, charset), Data.class);
URLResponse response = URLRequest.build(url, handler)
.header(HeaderNames.CONTENT_TYPE, HeaderValues.JSON)
.body(om.writeValueAsString(data))
.get();
Data body = response.getBody();
}
}```
* Set CookieManager.
```java
import pers.clare.urlrequest.URLRequest;
import pers.clare.urlrequest.URLResponse;import java.net.CookieManager;
import java.util.concurrent.Executors;class Example {
public static CookieManager cookieManager = new CookieManager();public static void main(String[] args) {
URLResponse response = URLRequest.build(url)
.header(HeaderNames.CONTENT_TYPE, HeaderValues.JSON)
.cookieManager(cookieManager)
.body(om.writeValueAsString(data))
.get();
String body = response.getBody();
}
}```
* Reusable
```java
import pers.clare.urlrequest.URLRequest;
import pers.clare.urlrequest.URLResponse;import java.util.concurrent.Executors;
class Example {
public static void main(String[] args) {
URLRequest request = URLRequest.build(url)
.header(HeaderNames.CONTENT_TYPE, HeaderValues.JSON)
.body(om.writeValueAsString(data));URLResponse response = request.get();
// can reuse
response = request.get();
}
}```