Android 请求https 网址

来源:互联网 发布:java操作hive sql 编辑:程序博客网 时间:2024/06/05 20:15

需要引入的包
import javax.net.ssl.HttpsURLConnection;import org.json.JSONObject;        String url="https://.......";    //https请求地址        // 获取请求结果private String getHttps(String _url) throws Exception {String _string = null;URL myURL = new URL(_url);HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();if (httpsConn.getResponseCode() == HttpsURLConnection.HTTP_OK) {// 取得该连接的输入流,以读取响应内容InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());_string = convertStreamToString(insr);}return _string;}// 将InputStream流转换成Stringpublic String convertStreamToString(InputStreamReader inputStream)throws IOException {if (inputStream != null) {Writer writer = new StringWriter();char[] buffer = new char[1024];try {Reader reader = new BufferedReader(inputStream, 1024);int n;while ((n = reader.read(buffer)) != -1) {writer.write(buffer, 0, n);}} finally {inputStream.close();}return writer.toString();} else {return "";}}