位置: 编程技术 - 正文

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 应用的内存使用情况(如何检查自己是否抑郁)

  • 个体工商户可以给自己交五险一金吗
  • 公允价值下降属于资产吗
  • 交易性资产入账金额和入账金额区别
  • 销售费用变动率税收政策
  • 劳务费专票需要备注吗
  • 清算期间,公司是否可以经营
  • 营改增后哪些费用可以抵扣
  • 子公司注销资产负债表如何合并
  • 电子商业承兑汇票
  • 慈善机构捐赠
  • 企业所得税前列支的费用
  • 关联企业无偿使用土地
  • 2021年路桥费抵扣最新政策
  • 特许权使用费个税计算公式
  • 金税盘抵减税额怎么算
  • 违约金收税吗
  • 软件开发公司研发主管绩效考核
  • 苹果系统怎么修复
  • 工会经费会计分局
  • 短期借款应付利息列报
  • 错账按产生原因来看有两种
  • 企业银行存款如何管理
  • 水电费无发票如何做账
  • 医疗机构医疗服务自查报告怎么写
  • 进口关税增值税计算公式
  • 腾讯电脑管家中的软件市场怎么拖到桌面
  • lsass.exe在哪个文件夹
  • php正则表达式匹配字符串
  • 工业企业成本核算的内容是
  • php如何调用类
  • thinkphp 模块
  • php7.3安装
  • 工业企业制造费用包括
  • php验证码扭曲效果怎么做
  • javaweb项目登录页面不跳转
  • zabbix 执行命令
  • 修改Laravel5.3中的路由文件与路径
  • 公交卡充值发票报销单怎么填
  • 工业企业成本核算方法
  • 研发费用没有发票怎么做账
  • mysql_assoc
  • 企业无票支出该怎么做账
  • 小规模纳税人1%税率优惠政策
  • 会计科目累计摊销
  • sql语句批量更新
  • 固定资产清理的借贷方向表示什么
  • 劳务派遣人员的档案会保存在用人单位吗
  • 一般纳税人增值税怎么做账务处理
  • 应收账款增加给哪一方
  • 员工借款未还财务有责任吗
  • 快递公司结算员能学到财务知识吗
  • 电子承兑汇票是24小时签收吗
  • 车辆保险费算什么费用
  • 小微企业免税如何做账
  • t3用友年底结束怎么建下一年
  • 职工住房补贴能否计入工资总额在税前扣除?
  • 税控盘开发票怎么测试打印机?
  • sql常见的数据类型有哪些
  • Linux下mysql源码安装笔记
  • windows开机提示无法登录到你的账户
  • Windows Server 2008故障转移群集简介
  • win8如何卸载360安全卫士?
  • linux tar压缩文件命令
  • win10怎么变回系统默认字体
  • win10自带的杀软叫什么
  • 各种linux系统比较
  • win1021年更新
  • 统计动态分析
  • cocos html
  • jquery ztree实现右键收藏功能
  • shell中删除文件和目录
  • nodejs怎么读
  • 深入理解新发展理念,推进供给侧结构性改革心得体会
  • 怎么利用python爬虫爬数据
  • unity unite
  • 2020宜兴市民中心营业时间
  • 福建地方税务局招聘
  • 乌鲁木齐税收政策
  • 余杭区税务局地址
  • 苏通卡上海服务网点查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设