package test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ClientProxy {
private static String PROXY_HOST="";
private static int PROXY_PORT=8080;
private static String PROXY_USERNAME="";
private static String PROXY_PASSWORD="";
private static String PROXY_WORKSTATION="";
private static String PROXY_DOMAIN="";
private static String TARGET_HOST="";
private static String TARGET_GETURL="/index.html";
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
AuthScope authscope=new AuthScope(PROXY_HOST, PROXY_PORT);
Credentials credentials=new NTCredentials(PROXY_USERNAME,PROXY_PASSWORD,PROXY_WORKSTATION,PROXY_DOMAIN);
httpclient.getCredentialsProvider().setCredentials(authscope,credentials);
HttpHost targetHost = new HttpHost(TARGET_HOST);
HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet httpget = new HttpGet(TARGET_GETURL);
System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost);
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
httpclient4.x配置带用户名和密码的代理
最新推荐文章于 2023-10-31 15:48:07 发布