From 2c81e10652be1e09cd9d935aef9d55f0bc867847 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 21 Aug 2023 09:27:38 +0100 Subject: [PATCH] Add support set basic proxy authentication. --- reqwest-jni/src/lib.rs | 11 +++++++++++ src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/reqwest-jni/src/lib.rs b/reqwest-jni/src/lib.rs index 49e6690..c490f28 100644 --- a/reqwest-jni/src/lib.rs +++ b/reqwest-jni/src/lib.rs @@ -15,6 +15,8 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_init( mut env: JNIEnv, _: JClass, proxy: JString, + user: JString, + pass: JString, ) { let builder = Client::builder() .user_agent("Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"); @@ -23,6 +25,15 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_init( Ok(proxy) => { let proxy = proxy.to_str().unwrap(); let proxy = reqwest::Proxy::all(proxy).unwrap(); + let proxy = match env.get_string(&user) { + Ok(user) => { + let user = user.to_str().unwrap(); + let pass = env.get_string(&pass).unwrap(); + let pass = pass.to_str().unwrap(); + proxy.basic_auth(user, pass) + } + Err(_) => proxy, + }; builder.proxy(proxy) } Err(_) => builder, diff --git a/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java b/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java index c140b35..7bc4273 100644 --- a/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java +++ b/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java @@ -53,7 +53,7 @@ public class ReqwestUtils { System.load(nativeFile.getAbsolutePath()); } - public static native void init(String proxy); + public static native void init(String proxy, String user, String pass); public static native CompletableFuture fetch(String url, String method, byte[] body, Map headers);