import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; public class HttpClientExample { public static void main(String[] args) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://www.example.com"); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { String responseBody = method.getResponseBodyAsString(); System.out.println(responseBody); } else { System.out.println("Unexpected status code: " + statusCode); } } catch (HttpException e) { System.out.println("HttpException occurred: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println("IOException occurred: " + e.getMessage()); e.printStackTrace(); } finally { method.releaseConnection(); } } } import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; public class HttpClientExample { public static void main(String[] args) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://www.example.com"); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultMethodRetryHandler(3, false)); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { String responseBody = method.getResponseBodyAsString(); System.out.println(responseBody); } else { System.out.println("Unexpected status code: " + statusCode); } } catch (HttpException e) { System.out.println("HttpException occurred: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println("IOException occurred: " + e.getMessage()); e.printStackTrace(); } finally { method.releaseConnection(); } } } client.getHostConfiguration().setProxy("proxyhost", proxyPort); Credentials credentials = new UsernamePasswordCredentials("username", "password"); client.getState().setCredentials(AuthScope.ANY, credentials);


上一篇:
下一篇:
切换中文