android用https发送post请求
来源:互联网 发布:知乎 恋人之间矛盾 编辑:程序博客网 时间:2024/04/20 12:04
原文地址:android用https发送post请求作者:长春
package acg.iptv.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
importorg.apache.http.conn.ssl.AllowAllHostnameVerifier;
public class HttpClient {
private static final AllowAllHostnameVerifierHOSTNAME_VERIFIER = new AllowAllHostnameVerifier();
private static X509TrustManager xtm = new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain, StringauthType) {}
private static X509TrustManager[] xtmArray =new X509TrustManager[] { xtm };
private static HttpsURLConnection conn=null;
public static InputStream sendPOSTRequestForInputStream(Stringpath, Map<String, String> params,String encoding) throws Exception{
// 1> 组拼实体数据
//method=save&name=liming&timelength=100
StringBuilder entityBuilder = new StringBuilder("");
if(params!=null &&!params.isEmpty()){
for(Map.Entry<String, String>entry : params.entrySet()){
entityBuilder.append(entry.getKey()).append('=');
entityBuilder.append(URLEncoder.encode(entry.getValue(),encoding));
entityBuilder.append('&');
}
entityBuilder.deleteCharAt(entityBuilder.length() - 1);
}
byte[] entity = entityBuilder.toString().getBytes();
URL url = new URL(path);
conn = (HttpsURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允许输出数据
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
outStream.flush();
outStream.close();
if(conn.getResponseCode() == 200){
return conn.getInputStream();
}
return conn.getInputStream();
}
public static void closeConnection(){
if (conn!=null)
conn.disconnect();
}
}
0 0
- android用https发送post请求
- android用https发送post请求
- https发送post请求
- android发送post请求
- Android发送post请求
- Android发送post请求
- Android发送post请求
- Android发送post请求
- Java 发送https 的post请求方法
- HttpClient向HTTPS地址发送POST请求
- java发送https post请求实例
- java 向HTTPS地址发送POST请求
- PHP CURL 发送HTTPS POST请求
- php curl扩展发送https post请求
- 发送post请求到https的url
- java发送https post请求实例
- java向https链接发送post请求
- 发送HTTP和HTTPS(单向验证)请求(包含POST请求)
- WampServer简单配置
- android listview addHe…
- PHP-ExcelReader个人总结
- android引入第三方jar包时报错java…
- PHP实现异步调用方法
- android用https发送post请求
- jQuery Mobile 手动显示…
- android2.2系统自带drawable资源
- ThinkPHP的Session操作
- Android系统设置
- sqlserver2008安装遇到的问题
- JS刷新父窗口的几种方式
- 【android】scaleType属性与ImagVi…
- adb无线网络调试