位置: 编程技术 - 正文

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

  • 进项税额比销项税额多怎么填申报表
  • 工资储备金制度
  • 建筑行业有哪种职业
  • 分期付款的消费税按实际收入算吗对吗
  • 交增值税及附加税怎么做账
  • 会计凭证借贷方向
  • 建设工程劳务分包的规定
  • 股份有限公司个人所得税缴纳比例
  • 当月没有收入,发生的人工和费用怎么办
  • 归属于母公司的净资产
  • 修理办公用复印机好吗
  • 预付账款是付没付钱
  • 工业企业用电是收入的几倍
  • 下个月的发票可以报销上个月的费用吗
  • 购货方跨月进项税额转出分录
  • 租入生物性资产如何入账
  • 税控系统技术维护费
  • 按季度计提利息
  • 所有利息收入都免税吗
  • 应退税款抵扣欠缴税款
  • 未按规定期限办理身份证
  • 预提费用年底必须结转吗
  • 暂估入账的处理
  • 银行余额调节表的作用
  • PHP:preg_match_all()的用法_PCRE正则函数
  • PHP:base64_decode()的用法_url函数
  • 截图快捷键ctrl+alt+
  • 企业研发费用的优惠政策
  • 煤炭企业的会计科目
  • 一只棕色
  • 长期借款汇兑收益怎么算
  • 劳务费用 税
  • 进项税加计抵扣10%和15%
  • golang、python、php、c++、c、java、Nodejs性能对比
  • php读取excel文件
  • 激光雷达lidar特点
  • php static变量
  • 发票中食品属于哪一类
  • 勾选发票提交后如何
  • 哪些研发费用可以资本化
  • 企业办理工程价款流程
  • 股权转让怎么查
  • mongodb启动命令 linux
  • linux mongodb配置文件
  • sqlcode错误码100
  • 电子税务局网开电子发票
  • 运输服务的增值税税率到底是6还是9
  • 所得税预缴申报表资产总额怎么填
  • 代扣员工伙食费
  • 物流辅助服务是
  • 公司帐户转到法人帐户用途写备用金行吗
  • 补计提所得税怎么做分录
  • 企业取得的跨期业务
  • 收入分成的账务处理
  • 电费发票开据后如何入帐?
  • 支付的劳务派遣服务费计入什么科目
  • 客户重复付款了怎么礼貌回复
  • 技术人员工资条
  • Navicat连接MySQL报错
  • windows2003远程桌面服务
  • win7系统玩游戏
  • windows10网页打不开怎么办
  • win10笔记本连接不了wifi
  • 系统更新win10
  • win10推送win11
  • lnmgr.exe是什么
  • win10系统免费升级
  • Android游戏开发实训总结
  • bat批处理命令大全
  • cocos2dx 3.2 Http网络连接,curl 库的介绍
  • dos命令到一个文件夹
  • nodejs excel转json
  • js鼠标滑动特效
  • javascript.
  • fragment
  • Python的requests网络编程包使用教程
  • 广东增值税电子专用发票
  • 纳税工会经费申请怎么写
  • 报fob价格最后谁退税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设