java http get post請求 https請求實現

來源:互聯網
上載者:User

直接貼代碼



public class HttpsAndHttp {

//https請求實現
public static StringBuffer httpsSendGet() 
   {  
   StringBuffer sb = new StringBuffer(); 
       String path = "https://www.12306.cn/";  
       TrustManager[] trustAllCerts = new TrustManager[]{ 
               new X509TrustManager() { 
                   public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
                       return null; 
                   } 
                   public void checkClientTrusted( 
                       java.security.cert.X509Certificate[] certs, String authType) { 
                   } 
                   public void checkServerTrusted( 
                       java.security.cert.X509Certificate[] certs, String authType) { 
                   } 
               } 
           }; 
      try { 
               SSLContext sc = SSLContext.getInstance("SSL"); 
               sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
               HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
           } catch (Exception e) { 
           } 
      try { 
           URL url = new URL(path); 
           HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); 
           BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); 
           String line; 
           while ((line = rd.readLine()) != null) { 
            sb.append(line);
           } 
          rd.close(); 
           } catch (MalformedURLException e) { 
           } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 
           return sb;
   }
public static void httpGet(String url){
System.out.println(url);
//構造HttpClient的執行個體
HttpClient httpClient = new HttpClient();
//建立GET方法的執行個體
GetMethod getMethod = new GetMethod(url);
//使用系統提供的預設的恢複策略
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();
//處理內容
String resposes = new String(responseBody,"utf-8");
System.out.println("http response:"+resposes);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  finally {
//釋放串連
getMethod.releaseConnection();
}
}
public static int httpPost(){
int i = 1;
String url ="http://www.baidu.com";
HttpClient httpClient = new HttpClient();
//建立GET方法的執行個體

PostMethod psotMethod = new PostMethod(url);
//psotMethod.setParameter("r", “222" );//這裡添加參數
psotMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");//請求編碼
//使用系統提供的預設的恢複策略
try {
//執行getMethod
int statusCode =  httpClient.executeMethod(psotMethod); 
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ psotMethod.getStatusLine());
}
//讀取內容 
byte[] responseBody = psotMethod.getResponseBody();
//處理內容
//System.out.println(new String(responseBody));
String resposes = new String(responseBody,"utf-8");
System.out.println("http response:"+resposes);//擷取到了連結 自己處理
//System.out.println("http response:"+resposes[1]);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}  finally {
//釋放串連
psotMethod.releaseConnection();
}
return i;
}
public static void main(String[] args) {
httpPost();
}
}


jar包地址

http://download.csdn.net/detail/huidanyige/8603109

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.