Android HttpURLConnection Post 参数 (https)
2015-12-30 15:29
555 查看
声明utf-8:
eg:登陆请求方法,通过接口返回结果:
post请求执行前,网络判断,以及url参数转换:
map转换成string类型的url参数:
post执行:(注:这里简单的描述了下接口返回参数而已)。
实例化HttpURLConnection post请求时,ssl添加权限:
添加证书管理:
参考:
Android SSL - No Peer Certificate
Javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: Failure in SSL library, usually a protocol error
http请求No peer certificate的解决方法(使用的是HTTPClient)
https 用 HttpsURLConnectio如何登陆 post方式
使用HttpsURLConnection发送POST请求
public static String CHARSET_UTF8 = HTTP.UTF_8;
eg:登陆请求方法,通过接口返回结果:
public static void login(String username, String password, ResponseCallbackHandler responseCallbackHandler) { Map<String, String> params = new HashMap<>(); params.put("username", username); params.put("password", password); post("https:/..../login", params, responseCallbackHandler); }
post请求执行前,网络判断,以及url参数转换:
private static void post(final String url, Map<String, String> _params, final ResponseCallbackHandler responseCallbackHandler) { if (!NetworkUtils.isNetworkConnected(RunnerApp.getContext())) { if (responseCallbackHandler != null) { responseCallbackHandler.onNetworkError(); responseCallbackHandler.onFinish(); } return; } final String params = map2Url(_params); new Thread(new Runnable() { @Override public void run() { post(url, params, responseCallbackHandler); } }).start(); }
map转换成string类型的url参数:
/** * map转url参数 */ public static String map2Url(Map<String, String> paramToMap) { if (null == paramToMap || paramToMap.isEmpty()) { return null; } StringBuffer url = new StringBuffer(); boolean isfist = true; for (Map.Entry<String, String> entry : paramToMap.entrySet()) { if (isfist) { isfist = false; } else { url.append("&"); } url.append(entry.getKey()).append("="); String value = entry.getValue(); if (!TextUtils.isEmpty(value)) { try { url.append(URLEncoder.encode(value, CHARSET_UTF8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } return url.toString(); }
post执行:(注:这里简单的描述了下接口返回参数而已)。
/** * post请求 */ public static void post(String url, String params, ResponseCallbackHandler responseCallbackHandler) { StringBuffer bufferRes = null; try { HttpURLConnection http = initHttps(url, "POST", null); OutputStream out = http.getOutputStream(); out.write(params.getBytes(CHARSET_UTF8)); out.flush(); out.close(); InputStream in = http.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(in, CHARSET_UTF8)); String valueString = null; bufferRes = new StringBuffer(); while ((valueString = read.readLine()) != null) { bufferRes.append(valueString); } in.close(); if (http != null) { http.disconnect();// 关闭连接 } if(responseCallbackHandler != null) { responseCallbackHandler.onSuccess(bufferRes.toString()); LogUtils.d(tag, "onSuccess"); LogUtils.d(tag, "onSuccess:" + bufferRes.toString()); } } catch (Exception e) { e.printStackTrace(); if (responseCallbackHandler != null) { responseCallbackHandler.onNetworkError(); } } if (responseCallbackHandler != null) { responseCallbackHandler.onFinish(); } }
实例化HttpURLConnection post请求时,ssl添加权限:
/** * 初始化http请求参数 */ private static HttpsURLConnection initHttps(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { TrustManager[] tm = {new CustomX509TrustManager()}; SSLContext sslContext = SSLContext.getInstance("TLSv1"); sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = sslContext.getSocketFactory(); URL _url = new URL(url); HttpsURLConnection http = (HttpsURLConnection) _url.openConnection(); // 连接超时 http.setConnectTimeout(25000); // 读取超时 --服务器响应比较慢,增大时间 http.setReadTimeout(25000); http.setRequestMethod(method); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"); if (null != headers && !headers.isEmpty()) { for (Map.Entry<String, String> entry : headers.entrySet()) { http.setRequestProperty(entry.getKey(), entry.getValue()); } } http.setSSLSocketFactory(ssf); http.setDoOutput(true); http.setDoInput(true); http.connect(); return http; }
添加证书管理:
// 证书管理 class CustomX509TrustManager implements X509TrustManager { public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }
参考:
Android SSL - No Peer Certificate
Javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: Failure in SSL library, usually a protocol error
http请求No peer certificate的解决方法(使用的是HTTPClient)
https 用 HttpsURLConnectio如何登陆 post方式
使用HttpsURLConnection发送POST请求
相关文章推荐
- CentOs网络管理:ifconfig家族,iproute家族及配置文件
- 关于 UITableView 中 网络获取图片 cell 自适应高度的问题
- Android广告条效果--使用Volley实现网络图片的自动轮播(仿淘宝、网易广告页轮播效果)
- 2015年直通车新玩法,你知道嘛!!
- 完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三
- 初步使用HttpURLConnection
- 新手写网络爬虫之初体验
- HttpClient和WebService的区别和介绍
- Android 通过hosts屏蔽指定ip的网络访问
- http 之session和cookie
- 尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost
- (二)卷积神经网络
- Linux网络配置管理
- 网络管理if,ip.ss
- 通过HttpClinet类用get方法获得远程接口返回的Json串
- android http请求 史上安卓功能最全的http请求框架(带缓存)
- iOS9 HTTP请求失效
- tcpdump 两个通信进程在同一台机子
- Linux下SVN服务器同时支持Apache的http和https及svnserve独立服务器三种模式且使用相同的访问权限账号
- hoRTC网络视频加密电话原理测试版使用说明