Android http https访问网络及资源
2015-04-23 17:26
399 查看
Http
View Code
private String getInputJson(String urlString, String userName, String token) { try { urlString = urlString + userName + "&token=" + token; Log.i(TAG, "发送请求链接:" + urlString); HttpURLConnection conn = null; URL url = new URL(urlString); // 关键代码 // ignore https certificate validation |忽略 https 证书验证 if (url.getProtocol().toUpperCase().equals("HTTPS")) { trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) url .openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); conn = https; } else { conn = (HttpURLConnection) url.openConnection(); } // conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setConnectTimeout(8 * 1000); conn.setDoOutput(true); conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); // 发送用户名参数 // String param = userNameAndTokenString; // byte[] out = param.getBytes(); // conn.getOutputStream().write(out); // conn.getOutputStream().flush(); String location = conn.getRequestProperty("location"); int resCode = conn.getResponseCode(); conn.connect(); InputStream stream = conn.getInputStream(); byte[] data = new byte[102400]; int length = stream.read(data); httpStr = new String(data, 0, length); conn.disconnect(); Log.i(TAG, httpStr); stream.close(); } catch (Exception ee) { Log.i(TAG, "error:" + ee.getMessage()); httpStr = ""; } return httpStr; }
View Code
相关文章推荐
- Android上Java网络资源访问(二)使用HttpURLConnection多线程下载
- Android网络访问之http、https、 cookie的使用
- Android网络编程之使用HTTP访问网络资源
- Android网络编程之使用HTTP访问网络资源
- Android进阶2之Https访问网络资源(获取网络图片)
- Android进阶2之Https访问网络资源(获取网络图片)
- Android网络编程之使用HTTP访问网络资源
- android 访问网络二 (基于httpurlconnection post)
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络资源
- 一个android访问http资源的便捷工具类——HttpHelper
- android HttpClient 访问https
- [转]Android访问网络,使用HttpURLConnection还是HttpClient
- Android之loadUrl访问网络资源与本地资源的方法
- Android学习--使用url访问网络资源
- 通过HTTP访问网络资源
- Android之B面试题④访问网络,使用HttpURLConnection还是HttpClient?
- Android网络编程之使用HTTP訪问网络资源
- Android 之 HttpURLConnection 访问网络
- Android访问网络,使用HttpURLConnection还是HttpClient?