转载声明:版权规文章原创作者所有 转载时间:2007年07月20日
转载作者:pablo3518
import java.io.IOException;
import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod;
/** * * @author jiawu,jiang * */ public class Httpclient {
private static String url ="http://www.163.com"; /** * @param args */ public static void main(String[] args) {
HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); //代理的用户名和密码 UsernamePasswordCredentials creds = new UsernamePasswordCredentials("", ""); //代理服务器 //client.getState().setCredentials( new AuthScope("192.168.0.254",3128),creds); client.getState().setProxyCredentials(AuthScope.ANY, creds); HostConfiguration hcf =new HostConfiguration (); hcf.setProxy("192.168.0.254",3128); //method.setDoAuthentication( true ); try { int statusCode = client.executeMethod(hcf,method); System.out.println(""); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody(); System.out.println(new String(responseBody)); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { method.releaseConnection(); } } }
|