`

httpclient4.x配置带用户名和密码的代理

    博客分类:
  • java
阅读更多

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();
        }
    }
}

分享到:
评论
2 楼 bewithme 2013-08-12  
HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )
Response content length: 3997
1 楼 bewithme 2013-08-12  
我使用这个方法时会报严重: Proxy authentication error: Invalid name provided (Mechanism level: Could not load configuration file D:\WINDOWS\krb5.ini (系统找不到指定的文件。))
这个错,,
但我用包来实现却没有问题,请指教呀大侠,QQ359709421
org.apache.commons.httpclient.HttpClient

相关推荐

    HttpClient 3.x to HttpComponents HttpClient 4.x

    例如,在HttpClient 3.x中,代码可能会使用`***mons.httpclient.HttpClient`类和`***mons.httpclient.methods.GetMethod`等,而在4.x版本中,这些都被新的API所替代。程序员需要熟悉`org.apache....

    httpclient 4.X版本下载有的带源码包

    2013-11-21 15:31 292,890 httpclient-4.0.2.jar 2017-12-20 12:08 351,132 httpclient-4.1.1.jar 2012-08-03 01:45 451,595 httpclient-4.1.2-sources.jar 2012-08-03 01:44 352,254 httpclient-4.1.2.jar 2012-08-...

    httpclient4.x官方范例

    在4.x版本中,HTTPClient进行了重大更新,与之前的3.x版本相比,它引入了更多的功能、优化和改进。本文将深入探讨HTTPClient 4.x的核心特性,并基于提供的"httpclient4.1_examples"示例文件,解析如何使用这个库进行...

    HTTPClient 4.3.X

    描述中提到的链接指向了一个ITEYE博客,虽然内容未给出,但可以推测博主可能分享了关于使用HTTPClient 4.3.X的一些经验,可能包括配置,最佳实践,或者是遇到的问题及解决方案。 标签 "源码" 暗示我们可能会讨论到...

    httpclient4.3.x及其依赖jar包

    HttpClient 4.3.x是该库的一个重要版本,它提供了丰富的功能和改进,使得开发者能够更方便地与Web服务器进行交互。在本文中,我们将深入探讨HttpClient 4.3.x的核心特性、依赖关系以及如何在实际项目中应用。 **...

    Java HttpClient 4.x Jar包

    含tutorial,commons-logging-1.1.1, commons-codec-1.4-bin.zip, httpcomponents-client-4.0.3-bin.zip, httpcomponents-core-4.1-bin.zip

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    httpclientDemo(4.x与4以下的版本)例子

    这个"HttpclientDemo(4.x与4以下的版本)"示例显然涵盖了HttpClient在不同版本间的使用方法,特别是4.x版本和4.x之前的版本之间的差异。在本文中,我们将深入探讨HttpClient的基本概念、主要功能以及在不同版本中的...

    HttpClient4.x手动释放底层HTTP连接[借鉴].pdf

    httpclient.getConnectionManager().shutdown(); } } } ``` 在上面的代码中,我们首先创建了一个HttpClient实例,然后执行了一个HttpGet请求接着我们从响应中获取了HttpEntity,接着我们使用输入流读取响应的...

    HttpClient3.1.jar

    开发者可以通过导入这个JAR文件,使用HttpClient提供的类,如`org.apache.commons.httpclient.HttpClient`和`org.apache.commons.httpclient.methods.GetMethod`,来构建HTTP客户端应用程序。 3. **commons-codec....

    android原生POST、httpClient4.X实现向PHP服务器上传文件 源码

    《android原生POST、httpClient4.X实现向PHP服务器上传文件》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17565481,奶奶个熊,今天CSDN写个博客都写不成,格式啥啥的完全跟写的时候不...

    httpclient-4.5.2.jar.zip

    CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { ...

    HttpClient所需jar包(全) httpClient.4.13jar

    CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { ...

    httpclient4.4.1 and httpclient4.5.3.rar

    HttpClient 4.5.3是4.x系列的一个重要升级,它引入了更多新功能和改进,进一步增强了HttpClient的稳定性和可扩展性: - **HTTP/2支持**:HttpClient 4.5.3引入了对HTTP/2协议的支持,这是一个重要的进步,因为它...

    《httpClient 4.X与PHP服务器通信详解》 对应源码

    《httpClient 4.X与PHP服务器通信详解》博文对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17416417

    org.apache.commons.httpclient-3.1.jar

    《Apache Commons HttpClient 3.1:HTTP客户端编程的基石》 ...然而,需要注意的是,HttpClient 3.1已不再维护,最新的稳定版本为HttpClient 4.x,对于新项目建议使用更现代的版本以获取更好的性能和兼容性。

    httpclient4.5.jar

    httpclient4.5.jar开发架包,包含所需要依赖的架包 。

    commons-httpclient3.1.jar,commons-codec1.3.jar,commons-logging1.1.1.jar

    HttpClient 3.1是该库的一个旧版本,尽管现在已经被更新的HttpClient 4.x系列取代,但在很多老项目中仍然广泛使用。它允许开发者构建复杂的HTTP请求,比如POST、GET、PUT等,并可以处理各种HTTP响应。 2. **Apache ...

Global site tag (gtag.js) - Google Analytics