DEMO代码示例-Java

更新时间:2020-07-04 09:17

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
* 青果网络Qstack Demo
* @ClassName QstackHttpProxy
* @Description
* @Date 2020/04/30 10:40
**/


public class QstackHttpProxy {
public static void main(String[] args) throws Exception {
String proxyHost = ""; //申请到的IP
Integer proxyPort = ; //申请到的端口
String proxyUserName = ""; //账号AuthKey
String proxyPassword = ""; //密码AuthPwd
String destUrl = "http://myip.ipip.net"; //访问目标地址
HttpHost host = new HttpHost(proxyHost, proxyPort);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(proxyUserName, proxyPassword));
CloseableHttpClient client = HttpClients.custom()
.setDefaultCredentialsProvider(provider).build();
HttpGet httpGet = new HttpGet(destUrl);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)
.setSocketTimeout(6000).setProxy(host).build();
httpGet.setConfig(requestConfig);
HttpResponse httpResponse = client.execute(httpGet);
System.out.println("http代理请求状态码: " + httpResponse.getStatusLine());
System.out.println("http代理请求响应内容: " +EntityUtils.toString(httpResponse.getEntity()));
}
}

本文导读

客户热线:4008765417