package test;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
public class NoCertificationHttpsGetSample {
?public static void main(String[] args) {
??Protocol myhttps = new Protocol("https",
????new MySecureProtocolSocketFactory(), 443);
??Protocol.registerProtocol("https", myhttps);
??// 構造HttpClient的實例
??HttpClient httpClient = new HttpClient();
??// 創建GET方法的實例
??GetMethod getMethod = new GetMethod("??// 使用系統提供的默認的恢復策略
??getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
????new DefaultHttpMethodRetryHandler());
??try {
???// 執行getMethod
???int statusCode = httpClient.executeMethod(getMethod);
???if (statusCode != HttpStatus.SC_OK) {
????System.err.println("Method failed: "
??????+ getMethod.getStatusLine());
???}
???// 讀取內容
???byte[] responseBody = getMethod.getResponseBody();
???// 處理內容
???System.out.println(new String(responseBody));
??} catch (HttpException e) {
???System.out.println("Please check your provided http address!");
???// 發生致命的異常,可能是協議不對或者返回的內容有問題
???e.printStackTrace();
??} catch (IOException e) {
???// 發生網絡異常
???e.printStackTrace();
??} finally {
???// 釋放連接
???getMethod.releaseConnection();
??}
?}
}
posted on 2007-01-18 09:53
happytian 閱讀(715)
評論(0) 編輯 收藏