http.get proxy

How to send http.get request with connected proxy server?
It is desirable to be able to use login and password

try this

importPackage(Packages["okhttp3"]);
importClass(java.net.Proxy)
importClass(java.net.Proxy.Type)
importClass(java.net.InetSocketAddress)

let proxyHost = "192.168.10.2";
let proxyPort = 7890;

let userName = "ibozo"
let password = "notapassword"

let client = new OkHttpClient.Builder()
    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
    .build();

var credential = Credentials.basic(userName, password)

let request = new Request.Builder()
    .url("https://www.youtube.com")
    .header("Proxy-Authorization", credential)
    .build();
try {
    let response = client.newCall(request).execute()
    if (!response.isSuccessful()) {
        throw new IOException("Unexpected code " + response);
    }
    // handle response  
    let body = response.body().string()
    files.create("./body.html")
    files.write("./body.html",body)
    log("write complete --> ", files.path("./body.html"))
} catch (e) {
    log(e)
}
最后由 老用户 编辑

It should be able to implement proxy and server authentication, you may need to wrap the function yourself to make it easier to use. ( I haven't tried proxy server authentication )

@ibozo Thanks a lot, it works, ChatGPT gave about the same method but it didn't work correctly