Https联网工具类

Https联网工具类 get  post

调用时 只需传入url,和要提交的参数queryString  有cookie时也可以传入

放回的是字符串 连接方式我在Http里解决了你们只需要拼接对

拼接格式 路径:https://emall.licaike.com/weixin/web/bind?        参数:loginInit=loginInit&knowChannel=APP_LCK_ADR_KC

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import android.util.Log;

public class HttpService {

/** 连接或读取超时单位毫秒 */

private static final int CONNECTION_TIMEOUT = 30000;// 15000

protected static String httpGet(String url, String queryString, String cookie)

throws Exception {

if (isNullEmptyBlank(url)) {

return "url不能为空";

}

if (!isNullEmptyBlank(queryString)) {

url += ("?" + queryString);

}

URL urlPath = null;

HttpURLConnection conn = null;

InputStream is = null;

try {

urlPath = new URL(url);

i("httpGet", "urlPath>>>>>" + urlPath);

conn = (HttpURLConnection) urlPath.openConnection();

conn.setDoInput(true);

conn.setUseCaches(false);

conn.setInstanceFollowRedirects(true);

// 设置连接主机超时(单位:毫秒)

conn.setConnectTimeout(CONNECTION_TIMEOUT);

// 设置从主机读取数据超时(单位:毫秒)

conn.setReadTimeout(CONNECTION_TIMEOUT);

conn.setRequestProperty("Accept", "*/*");

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

conn.setRequestMethod("GET");

if (!isNullEmptyBlank(cookie)) {

conn.setRequestProperty("Cookie", cookie);

i("httpGet", "cookie>>>>>" + cookie);

}

if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {

is = conn.getInputStream();

String str = readData(is, "UTF-8");

e("httpGet", "str>>>>>" + str);

return str;

}

} catch (Exception e) {

throw new Exception(e.getMessage());

} finally {

try {

if (is != null)

is.close();

if (conn != null)

conn.disconnect();

} catch (Exception e) {

} finally {

is = null;

conn = null;

}

}

return null;

}

protected static String httpsPost(String url, String queryString, String cookie)

throws Exception {

if (isNullEmptyBlank(url)) {

return "url不能为空";

}

URL urlPath = null;

HttpsURLConnection conn = null;

OutputStream os = null;

InputStream is = null;

try {

SSLContext sslContext = SSLContext.getInstance("TLS");

sslContext.init(null,

new TrustManager[] { new MyX509TrustManager() },

new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext

.getSocketFactory());

HttpsURLConnection

.setDefaultHostnameVerifier(new MyHostnameVerifier());

urlPath = new URL(url);

conn = (HttpsURLConnection) urlPath.openConnection();

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

conn.setInstanceFollowRedirects(true);

// 设置连接主机超时(单位:毫秒)

conn.setConnectTimeout(CONNECTION_TIMEOUT);

// 设置从主机读取数据超时(单位:毫秒)

conn.setReadTimeout(CONNECTION_TIMEOUT);

conn.setRequestProperty("Accept", "*/*");

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

conn.setRequestMethod("POST");

if (!isNullEmptyBlank(cookie)) {

conn.setRequestProperty("Cookie", cookie);

i("httpsPost", "cookie>>>>>" + cookie);

}

if (!isNullEmptyBlank(queryString)) {

os = conn.getOutputStream();

os.write(queryString.getBytes("UTF-8"));

os.flush();

i("httpsPost", url + queryString);

}

if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {

is = conn.getInputStream();

String str = readData(is, "UTF-8");

e("httpsPost", "str>>>>>" + str);

return str;

}

} catch (Exception e) {

throw new Exception(e.getMessage());

} finally {

try {

if (os != null)

os.close();

if (is != null)

is.close();

if (conn != null)

conn.disconnect();

} catch (Exception e) {

e.printStackTrace();

} finally {

os = null;

is = null;

conn = null;

}

}

return null;

}

protected static String httpPost(String url, String queryString)

throws Exception {

if (isNullEmptyBlank(url)) {

return "url不能为空";

}

URL urlPath = null;

HttpURLConnection conn = null;

OutputStream os = null;

InputStream is = null;

try {

urlPath = new URL(url);

conn = (HttpURLConnection) urlPath.openConnection();

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

conn.setInstanceFollowRedirects(true);

// 设置连接主机超时(单位:毫秒)

conn.setConnectTimeout(CONNECTION_TIMEOUT);

// 设置从主机读取数据超时(单位:毫秒)

conn.setReadTimeout(CONNECTION_TIMEOUT);

conn.setRequestProperty("Accept", "*/*");

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

conn.setRequestMethod("POST");

if (!isNullEmptyBlank(queryString)) {

os = conn.getOutputStream();

os.write(queryString.getBytes("UTF-8"));

os.flush();

i("httpPost", url + "?" + queryString);

}

if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {

is = conn.getInputStream();

String str = readData(is, "UTF-8");

e("httpPost", "str>>>>>" + str);

}

} catch (Exception e) {

throw new Exception(e.getMessage());

} finally {

try {

if (os != null)

os.close();

if (is != null)

is.close();

if (conn != null)

conn.disconnect();

} catch (Exception e) {

e.printStackTrace();

} finally {

os = null;

is = null;

conn = null;

}

}

return null;

}

private static String readData(InputStream inSream, String charsetName)

throws Exception {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];

int len = -1;

while ((len = inSream.read(buffer)) != -1) {

outStream.write(buffer, 0, len);

}

byte[] data = outStream.toByteArray();

outStream.close();

inSream.close();

return new String(data, charsetName);

}

private static class MyHostnameVerifier implements HostnameVerifier {

public boolean verify(String hostname, SSLSession session) {

return true;

}

}

private static class MyX509TrustManager implements X509TrustManager {

public X509Certificate[] getAcceptedIssuers() {

return null;

}

public void checkServerTrusted(X509Certificate[] chain, String authType)

throws CertificateException {

}

public void checkClientTrusted(X509Certificate[] chain, String authType)

throws CertificateException {

}

};

private static void i(String tag, String msg) {

if (tag == null || msg == null) {

return;

}

Log.i(tag, msg);

}

private static void e(String tag, String msg) {

if (tag == null || msg == null) {

return;

}

Log.e(tag, msg);

}

/**

* 判断字符串是否为空(包含null与"","    ")

*

* @param str

* @return

*/

private static boolean isNullEmptyBlank(String str) {

if (str == null || "".equals(str) || "".equals(str.trim()))

return true;

return false;

}

}

时间: 2024-07-29 01:30:19

Https联网工具类的相关文章

我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEn

Https通信工具类

记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; /** * 证书信任管理器(用于https请求) */ public class MyX509TrustManager implements

https请求工具类

import java.io.IOException;import java.io.UnsupportedEncodingException;import java.lang.reflect.Field;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java

HttpClient联网工具类

public class HttpConnect { public static String getNews(String url,List<? extends NameValuePair> parameters) { StringBuilder sb = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpPost post=new HttpPost(url); HttpParams params =

Android-HttpsURLConnectionHelp工具类

Java版(HttpURLConnection)Https请求工具类 public class HttpsURLConnectionHelp { /** * 加密协议 */ public static TrustManager truseAllManager = new X509TrustManager() { public void checkClientTrusted( java.security.cert.X509Certificate[] arg0, String arg1) throw

捕获异常、存sd卡、封装请求头体、加密map值、网络工具类、生成Json、Https协议、传log日志到服务器、app崩溃友好重启

点击打开链接,免积分下载 在集成了统计SDK(友盟统计,百度统计等)之后,有一个非常有利于测试的功能:错误分析!此功能能够将程序在运行中碰到的崩溃(runtimeException)问题反馈到服务器,帮助开发者改善产品,多适配机器.然而在公司android开发中不集成这些SDK,那应该怎么实现这样的功能呢?下面让我们来看下如何使用UncaughtExceptionHandler来捕获异常. 在Android开发中,常常会出现uncheched Exception 导致程序的crash,为了提供良

基于HttpClient 4.3的可访问自签名HTTPS站点的新版工具类

本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始出处查看此文. HttpClient在当今Java应用中的位置越来越重要.从该项目的变迁过程我们不难发现,其已经从apache-commons众多的子项目中剥离,一跃成为如今的顶级项目,可见它的分量.然而随着项目的升级和架构的调整,很多以前常用的类和方法都已被打上了@Deprecated注解,

基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类

本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议在原始出处查看此文. HttpClient在当今Java应用中的位置越来越重要.从该项目的变迁过程我们不难发现,其已经从apache-commons众多的子项目中剥离,一跃成为现在的顶级项目.可见它的分量.然而随着项目的升级和架构的调整.非常多曾经经常使用的类和方法都已被打上了@Deprecated

发送http请求和https请求的工具类

package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.ht