位置: 编程技术 - 正文

Https联网工具类

编辑:rootadmin

推荐整理分享Https联网工具类,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

Https联网工具类 get post

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

Https联网工具类

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

拼接&#;式 路径: 参数: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.XCertificate;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.XTrustManager;import android.util.Log;public class HttpService { /** 连接或读取超时单位毫秒 */ private static final int CONNECTION_TIMEOUT = ;// 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 MyXTrustManager() }, 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[]; 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 MyXTrustManager implements XTrustManager { public XCertificate[] getAcceptedIssuers() { return null; } public void checkServerTrusted(XCertificate[] chain, String authType) throws CertificateException { } public void checkClientTrusted(XCertificate[] 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; }}

如何检查 Android 应用的内存使用情况 Android是为移动设备而设计的,所以应该关注应用的内存使用情况。尽管Android的Dalvik虚拟机会定期执行垃圾回收操作,但这也不意味着就可以忽视应用在

Android自动开关机实现详细教程 --------------------

Android实战--解析稍复杂JSON数据DEMO 废话不多说,直接上代码,布局文件:?xmlversion=1.0encoding=utf-8?LinearLayoutxmlns:android=

标签: Https联网工具类

本文链接地址:https://www.jiuchutong.com/biancheng/374499.html 转载请保留说明!

上一篇:[外文资料]利用SharedPreference管理Session(利用用英文怎么翻译)

下一篇:如何检查 Android 应用的内存使用情况(如何检查自己是否抑郁)

  • 退税现金流量表做哪里
  • 外贸企业出口退税流程图
  • 先包装后销售先销售后包装的消费税处理
  • 房地产增值税抵减土地出让金
  • 固定资产清理净损失计入什么科目
  • 制造费用多结转了下月如何调整
  • 个体户一季度利润28万用缴纳增值税吗
  • 出租的城镇土地由谁交城镇土地使用税
  • 房地产企业固定资产的折旧
  • 机动车丢失后怎么销户
  • 支付利息的诉讼请求
  • 一般纳税人可以转小规模吗
  • 建安企业增值税预缴
  • 营改增后银行增值税如何缴纳
  • 公积金抵扣个税上限
  • 农产品增值税免税政策
  • 网上办税大厅查询财务负责人?
  • 企业房产税如何计算缴纳
  • 计算企业所得税的公式
  • 增值税核算应设置的会计科目排序
  • 资本公积转增资本个人股东是否纳税
  • 小规模纳税人建筑行业
  • 生产酒的税收是多少
  • 海外佣金税务怎么缴纳
  • 出口货物退免税凭证资料应当保存几年
  • 公司购买进口产成品关税会计分录怎么做?
  • linux的使用场合
  • 苹果mac恢复出厂设置
  • 帮公司垫付的钱怎么要
  • 代扣代缴代收代缴税款业务内容
  • linux命令行怎么用
  • 不动产租赁费计入什么科目
  • 什么是主营业务税金及附加
  • 子公司使用母公司授信
  • 生产性生物资产折旧计入什么科目
  • 企业借给个人钱合法吗
  • 暂估成本的账务处理分录
  • php遍历文件夹
  • 外经证核销期限是多久
  • Anaconda(python,pycharm)半详细安装教程
  • 一般纳税人怎么申报增值税
  • 研发费用怎么体现
  • 公司租入厂房怎么做账
  • 进项税转出的会计分录
  • access数据库干嘛的
  • mongodb分页查询count太慢
  • 定期定额自行申报表应税项填多少
  • 特惠贷贴息金额怎么算的
  • 预付卡发票如何开
  • 关联企业债资比怎么计算
  • centos 安装方法
  • mysql简单操作
  • 小规模纳税人利润率一般是多少
  • 差旅费中火车票的进项税怎么计算
  • 未发货未收款先开具发票怎么账务处理
  • 活动经费要发到每个员工
  • 集团管理费收入是否纳税
  • sql server设置自增
  • 卸载微信后重新登录微信怎么恢复之前的数据
  • fedora安装双系统
  • win10预览版和正式版区别
  • 如何解决win10系统开机一直转圈圈的问题
  • Win10预览版镜像
  • linux用什么版本
  • linux mangle
  • android开发一般用什么软件
  • react native community
  • excel文档权限设置
  • bat ping批处理
  • node js并发加载缓慢
  • unity移动游戏开发
  • android view动画
  • python的异常处理语句
  • Android之BroadcastReceiver
  • python操作数据库语句
  • jquery中加载文档的方法
  • 提高税务干部七种能力的意义
  • 广州市地税局副局长
  • 无锡医疗保险缴费比例
  • 2011年退伍军人证
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设