調査した感じだと以下の3つが使えそう。
- DefaultHttpClientを利用する
- HttpsUrlConnectionを利用する
- AndroidHttpClientを利用する(2.2以降対応)
1.DefaultHttpClientを利用する
一応HttpHostと HttpGetで渡す例を載せた。(HttpHostの方はポートが指定できるから、汎用的に使えるかも)
HttpHostの例
Uri.Builder uriBuilder = new Uri.Builder(); uriBuilder.path("/index.html"); HttpClient client = new DefaultHttpClient(); try { HttpResponse httpResponse = client.execute(new HttpHost("www.hogehoge.jp", 443, "https"), new HttpPost(uriBuilder.build().toString())); String str = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); Log.d("__RESPONCE__",str); } catch (ClientProtocolException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); }
HttpGetの例
HttpClient client = new DefaultHttpClient(); try { HttpResponse httpResponse = client.execute(new HttpGet("https://www.nhk-ondemand.jp/index.html")); String str = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); // レスポンスコードのデバッグ表示 Log.d("__RESPONCE__",str); } catch (ClientProtocolException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); }
2.HttpsUrlConnectionを利用する
String href = "https://hogehoge/index.html"; try{ URLConnection connection = new URL(href).openConnection(); HttpsURLConnection httpsconnection = (HttpsURLConnection) connection; int responseCode = httpsconnection.getResponseCode(); } catch(IOException e){ e.printStackTrace(); }