用户名密码认证

使用提示

Socks代理使用样例
  1. http/https网页均可适用
  2. 运行环境要求 jdk >= 1.6
// 请求socks代理服务器(用户名密码认证)
// http和https网页均适用     

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.util.zip.GZIPInputStream;

public class JavaSocksAuth {

    public static void main(String[] args) throws IOException {
        // 演示IP,生产环境中请替换为提取到的代理信息
        String proxyIp = "115.219.7.84"; // 代理服务器IP
        int proxyPort = 44625;  // 代理服务器端口
        // 用户名密码认证
        String username = "username";  // 用户名
        String password = "password"; // 密码

        // 要访问的目标网页
        String pageUrl = "https://www.juliangip.com/api/general/Test";

        //确保使用用户名密码鉴权正常运行
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyIp, proxyPort));
        URL url = new URL(pageUrl);
        HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
        httpUrlConnection.setRequestMethod("GET");
        httpUrlConnection.setConnectTimeout(5*1000);  // 设置超时时间     
        httpUrlConnection.setRequestProperty("Accept-Encoding", "gzip");  // 添加gzip压缩让数据传输更块

        // 设置代理认证
        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(username,
                        password.toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);

        // 发起请求
        httpUrlConnection.connect();

        // 输出状态码
        System.out.println("code: " + httpUrlConnection.getResponseCode());

        // 读取返回内容
        InputStream inputStream = httpUrlConnection.getInputStream();
        String encoding = httpUrlConnection.getContentEncoding();
        // 处理gzip压缩
        if (encoding.equals("gzip")) inputStream = new GZIPInputStream(inputStream);
        String message = getContentFromInputStream(inputStream);

        // 输出返回内容
        System.out.println(message);

        // 关闭输入流和连接
        inputStream.close();
        httpUrlConnection.disconnect();
    }

    // 读取输入流中的内容
    public static String getContentFromInputStream(InputStream inputStream) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inputStream, "UTF-8");
        for (;;) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0) break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }
}
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。