Java
一、HTTP 代理示例
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
try {
String proxyHost = "us.ipwo.net";
int proxyPort = 7878;
Proxy proxy = new Proxy(
Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort)
);
String username =
"username_custom_zone_us";
String password = "password";
String credentials =
username + ":" + password;
URL url =
new URL("http://ipinfo.io");
HttpURLConnection connection =
(HttpURLConnection)
url.openConnection(proxy);
connection.setRequestMethod("GET");
connection.setRequestProperty(
"Proxy-Authorization",
"Basic " +
Base64.getEncoder()
.encodeToString(
credentials.getBytes()
)
);
int responseCode =
connection.getResponseCode();
System.out.println(
"Response Code: " +
responseCode
);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
connection.getInputStream()
)
);
String inputLine;
StringBuilder response =
new StringBuilder();
while (
(inputLine = in.readLine())
!= null
) {
response.append(inputLine);
}
in.close();
System.out.println(
response.toString()
);
} catch (IOException e) {
e.printStackTrace();
}
}
}二、正常返回示例
三、增加 Timeout(推荐)
四、常见问题
五、推荐开发建议
六、相关文章
最后更新于
这有帮助吗?
