以上代码通过Apache HttpComponents库,使用Java其中,proxy_host参数为代理服务器的主机名,proxy_port参数为服务器的端口号。程序首先创建了一个HttpGet对象,然后创建了一个HttpClient对象。接着,设置了HttpGet对象的URI为服务器的地址,然后执行请求。响应实体被获取并转换为字符串。最后,关闭所有资源。
// 导入Apache HttpComponents库
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
// 创建HttpGet对象
HttpGet httpGet = new HttpGet("https:///");
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 设置代理
httpGet.setURI(new URI("http://"));
// 发送请求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 获取响应实体
HttpEntity entity = response.getEntity();
// 将响应内容读取到字符串中
String content = EntityUtils.toString(entity);
// 关闭资源
response.close();
httpGet.close();
httpClient.close();