使ç¨ä»£çéè¦å¯¼å
¥ï¼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;
}