本博客之前的《像素化你的代码》文章中,我介绍了一种将文本生成图片的方法,还提供了一个 Encode/Decode工具。在那篇文章中,我写到「这又是一个除了好玩并没什么卵用的东西」,确实如此。不过,我今天想到一个用图片传递数据的另类应用场景,分享给大家。
在 Android 系统中,很多 App 为了在网页里实现一些客户端的功能,往往会在客户端开启一个本地 HTTP 服务。通过在网页中调用这个服务的不同接口,可以轻松实现原本无法实现的功能。本文不讨论这种做法本身,只说存在的问题:随着越来越多 Web 产品升级到 HTTPS,通过 JSONP 或 XHR2 与 HTTP 服务交互就行不通了。
下面这几段直接摘自我的《 关于启用 HTTPS 的一些经验分享(一)》这篇文章:
现代浏览器(Chrome、Firefox、Safari、Microsoft Edge),基本上都遵守了 W3C 的 Mixed Content规范,将 Mixed Content 分为 Optionally-blockable和 Blockable两类:
Optionally-blockable类 Mixed Content 包含那些危险较小,即使被中间人篡改也无大碍的资源。现代浏览器默认会加载这类资源,同时会在控制台打印警告信息。这类资源包括:
- 通过
标签加载的图片(包括 SVG 图片);
- 通过
- 预读的(Prefetched)资源;
除此之外所有的 Mixed Content 都是 Blockable,浏览器必须禁止加载这类资源。所以现代浏览器中,对于 HTTPS 页面中的 JavaScript、CSS 等 HTTP 资源,一律不加载,直接在控制台打印错误信息。
也就是说对于移动端浏览器,除非将本地 HTTP 服务升级为 HTTPS,否则在 HTTPS 网页中创建 script 标签或者发送 XHR2 请求调用本地服务,默认都会被阻止。
那么,有没有其它数据交互方案可以绕过 Mixed Content 限制呢?显然,图片类 HTTP 资源属于 Optionally-blockable类 Mixed Content,现代浏览器默认不阻止他们加载,只会在控制台打印警告,地址栏不显示小绿锁而已,可以好好利用。
所以,网页端单向通知 App,不需要拿返回值时,直接把本地接口请求改用 Image 发送即可。
如果需要返回值,那就可以用我之前的方案将文本编为图片,成功加载后解码图片即可还原内容。这里要注意一点:给图片响应配置 CORS 头之后,才能在 Canvas 中读到它的像素点数据。
原理差不多就是这些,下面是一个例子(使用阅读器的同学请点到原文查看):
这个例子演示了如何通过图片在 HTTPS 网站中获取 HTTP 接口数据,用到了 Canvas。实际上,如果返回值可枚举,连 Canvas 也可以省了 —— 服务端直接生成宽 1px,高 npx 的图片,JS 获取图片高度即可知道返回值。
本文链接: https://imququ.com/post/use-image-to-transfer-data.html, 参与评论。
-- EOF--
发表于 2016-03-10 00:38:48,并被添加「canvas、HTTPS」标签。
本站部署于「 阿里云 ECS」。如果你也要购买阿里云服务,可以使用我的九折推荐码 NY1Z0E (限新用户),多谢支持!

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools