java HttpClient设置代理的话,用户名和密码怎么设置?

HttpClient client = new HttpClient();
//代理但是如果需要用户名呢?
client.getHostConfiguration().setProxy(userData.proxyIP, userData.proxyPort)
希望做过的同学帮帮我,随便copy或是没验证的就算了。。。。最好是做过的,呵呵

使用代理需要导入:commons-logging-1.1.jar,httpclient-4.0-beta2.jar ,httpcore-4.1-alpha1.jar 和 commons-codec-1.4.jar架包。

在连接代理时需要使用用户名和密码构造UsernamePasswordCredentials对象并作为参数传递给HttpClient对象。


具体用法如下:

public static void main(String args[])
{
 StringBuffer sb = new StringBuffer();
 //创建HttpClient实例
 HttpClient client = getHttpClient();
 //创建httpGet
 HttpGet httpGet = new HttpGet("http://www.csdn.net");
 //执行
 try {
  HttpResponse response = client.execute(httpGet);
  
  HttpEntity entry = response.getEntity();
  
  if(entry != null)
  {
   InputStreamReader is = new InputStreamReader(entry.getContent());
   BufferedReader br = new BufferedReader(is);
   String str = null;
   while((str = br.readLine()) != null)
   {
    sb.append(str.trim());
   }
   br.close();
  }
  
 } catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 System.out.println(sb.toString());
}
//设置代理
public static HttpClient getHttpClient() {
 DefaultHttpClient httpClient = new DefaultHttpClient();
 String proxyHost = "proxycn2.huawei.com";
 int proxyPort = 8080;
 String userName = "china\\******";
 String password = "*******“
 httpClient.getCredentialsProvider().setCredentials(
   new AuthScope(proxyHost, proxyPort),
   new UsernamePasswordCredentials(userName, password));
 HttpHost proxy = new HttpHost(proxyHost,proxyPort);
 httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
 return httpClient;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-25
没用过这款软件,不过设置页面背景表现在代码background上就是:
1.在整个页面中,那就是body部分,类似如下:
body {
background:url(文件地址) no-repeat;
}
2.在网页中的标签部分,则是标签
标签 {
background:url(文件地址) no-repeat;
}
需要说明的是,那个no-repeat的位置是设置是否重复,以及在什么地方重复repeat-x;repeat-y等等,
第2个回答  2011-08-25
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userData.id, userData.password);
client.getState().setProxyCredentials(AuthScope.ANY, creds);本回答被提问者采纳
相似回答