Add support set basic proxy authentication.

This commit is contained in:
Kavin 2023-08-21 09:27:38 +01:00
parent 31bfff42e3
commit 2c81e10652
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F
2 changed files with 12 additions and 1 deletions

View file

@ -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,

View file

@ -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<Response> fetch(String url, String method, byte[] body,
Map<String, String> headers);