大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Base64;
public class Test {
public static void main(String[] args) {
try {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
System.setProperty("http.proxyHost", "82.157.163.144");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "82.157.163.144");
System.setProperty("https.proxyPort", "8080");
URL realUrl = new URL("https://www.ljxwtl.cn");
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestMethod("GET");
// 获取URLConnection对象对应的输出流
// flush输出流的缓冲
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class BasicAuthenticator extends Authenticator {
String userName;
String password;
public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* Called when password authorization is needed. Subclasses should
* override the default implementation, which returns null.
*
* @return The PasswordAuthentication collected from the
* user, or null if none is provided.
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
}
该实现方法可以放在多线程的main主线程中,依然起作用!!!
支持真正的http和真正有证书的https请求有效!!!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195302.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...