主要是设置http头
在HostConfiguration.setProxy里设置。如下面代码:
import java.io.IOException;   
import java.util.ArrayList;   
import java.util.List;   
  
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;   
import org.apache.commons.httpclient.Header;   
import org.apache.commons.httpclient.HttpClient;   
import org.apache.commons.httpclient.HttpException;   
import org.apache.commons.httpclient.HttpStatus;   
import org.apache.commons.httpclient.UsernamePasswordCredentials;   
import org.apache.commons.httpclient.auth.AuthScope;   
import org.apache.commons.httpclient.methods.GetMethod;   
import org.apache.commons.httpclient.params.HttpMethodParams;   
  
public class HttpClientUse {   
  
    public static void main(String[] args) throws HttpException, IOException {   
        HttpClient httpClient = new HttpClient();   
  
        httpClient.getHostConfiguration().setProxy("localhost", 808);   
  
        /*//需要验证  
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials("chenlb", "123456");  
 
        httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);  
        */  
  
        //设置http头   
        List<Header> headers = new ArrayList<Header>();   
        headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));   
        httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);   
  
        GetMethod method = new GetMethod("http://www.baidu.com");   
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,   
                new DefaultHttpMethodRetryHandler(3, false));   
        try {   
            int statusCode = httpClient.executeMethod(method);   
  
            if (statusCode != HttpStatus.SC_OK) {   
                System.out.println("Method failed code="+statusCode+": " + method.getStatusLine());   
  
            } else {   
                System.out.println(new String(method.getResponseBody(), "gb2312"));   
            }   
        } finally {   
            method.releaseConnection();   
        }   
    }   
}  
如果要用户名与密码验证的,请把/* */注释去掉。使验证有效。验证,关键是:UsernamePasswordCredentials creds = new UsernamePasswordCredentials("chenlb", "123456");   
  
httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);  
设置http请求头.List<Header> headers = new ArrayList<Header>();   
headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));   
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);  

解决方案 »

  1.   

    认证方案为Basic,可以成功。是在我本机测的,用的代理服务器软件.
    然后把程序给其他的人,其他人的代理服务器认证方案是NTLM的,不知道为什么不成功?
      

  2.   

    哦,谢谢。
    不过若认证方案是NTLM的不能用UsernamePasswordCredentials,而必须要用NTCredentials实例。
    NTCredentials实例我已经创建好了,且设置了setProxyCredentials。若是代理认证,httpclient会自己添加代理认证头部信息。自己添加不好实现,因为需要用到几种编码转化。