通过http发送二进制数据 [英] Sending binary data over http

查看:119
本文介绍了通过http发送二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关通过端口80从远程GPRS设备发送/接收数据的最佳方法的建议。



随机创建普通TCP套接字端口工作正常,但许多运营商只允许端口80 HTTP流量通过其代理,然后期望HTTP ascii数据(他们可以根据需要修改标头。



所以,我的设备应该在持久的http连接上创建POST请求,然后从Web服务接收base64编码的响应吗?我不确定当涉及二进制数据时移动代理的行为如何。是否有推荐的方法来执行此操作?



我可以调整设备的固件和服务器端应用程序。





我想知道是否有标准(或多或少)这样做。对于各种数据记录和工业系统,需要通过套接字连接发送大量二进制数据。对于以太网连接,通常有只有适应某些防火墙的问题,但持久的二进制连接在任意端口上建立都没有问题。



然而,移动互联网服务提供商倾向于限制他们的数据计划仅限80端口。他们还冒昧地使用HTTP标头,可能还有HTML数据本身。这是我需要确定潜在陷阱和规避它们的方法。




  • 只需发送base64编码数据吗?

  • 如何处理HTTP会话?任意套接字可以保持很长时间,但HTTP动词通常是短暂的。这是否意味着我需要为每个数据包创建一个新连接?或者有没有办法通过单个连接以块的形式发送服务器响应?

  • ISP代理在哪些方面会弄乱数据或标题?例如,代理有时可以保持连接活动,即使服务器关闭它。


解决方案

< blockquote>

只是简单地发送base64编码数据吗?


不需要使用base 64编码 - 这只会增加您必须传输的字节数。移动运营商通常会限制对他们理解的内容类型的响应(即图像,样式表等)。


如何处理HTTP会话?


HTTP会话通常通过URL查询参数或cookie值处理。但是,根据你所说的,它听起来并不像会话。


任意套接字可以保持很长时间,但HTTP动词通常是短暂的。这是否意味着我需要为每个数据包创建一个新连接?


HTTP请求可以持续任意长时间的时间,就像原始TCP套接字一样。如有必要,GET请求可持续数小时。您无需为每个请求创建新连接—看一下连接:Keep-Alive HTTP标题。


或者是有没有办法通过单个连接以块的形式发送服务器响应?


如果你不知道响应的长度你可以省略Content-Length标头,或者最好使用 Transfer-Encoding:chunked HTTP标头。


ISP代理以什么方式处理数据或标题?例如,代理有时可以保持连接处于活动状态,即使服务器关闭它也是如此。


ISP不会透露他们对HTTP响应所做的更改。如果您担心这一点,一个简单的解决方案是加密数据并指定 Content-Encoding HTTP标头。这将要求您控制HTTP客户端和服务器。


I am looking for suggestions on the best way to send/receive data from a remote GPRS device, over port 80.

Creating a plain TCP socket on a random port works fine, but many carriers only allow port 80 HTTP traffic through their proxies, and then expect HTTP ascii data (for which they can modify headers as needed.

So, should my device create a POST request on a persistent http connection, and then receive a base64 encoded response from the web service? I am not sure how mobile proxies behave when binary data is involved. Is there a recommended way to do this?

I can adapt both device's firmware and the server side app.

[Edit]

I would like to know if there is a standard (more or less) way to do this. For various data logging and industrial systems, there is a need to send lots of binary data over a socket connections. For Ethernet connections, there are usually only problems involved with adapting some firewalls, but persistent binary connections have no trouble being established over arbitrary ports.

Mobile ISPs, however, tend to limit their "data plans" for port 80 only. They also take the liberty to mess with HTTP headers, and potentially the HTML data itself. This is where I need to identify potential pitfalls and ways to circumvent them.

  • Will simply sending base64 encoded data work?
  • How are the HTTP sessions handled? Arbitrary sockets can be kept alive for a long time, but HTTP verbs are usually short lived. Does this mean I will need to create a new connection for each packet of data? Or is there a way to send server responses in chunks, over a single connection?
  • In what ways can an ISP proxy mess with the data, or the headers? For example, a proxy can sometimes keep a connection alive, even if the server closes it.

解决方案

Will simply sending base64 encoded data work?

There is no need to use base 64 encoding - this will simply increase the number of bytes you must transfer. Mobile operators normally limit mangling of responses to content types that they understand - i.e. images, stylesheets, etc.

How are the HTTP sessions handled?

HTTP sessions are normally handled either via a URL query parameter or via a cookie value. However, from what you have said it doesn't sound like sessions are necessary.

Arbitrary sockets can be kept alive for a long time, but HTTP verbs are usually short lived. Does this mean I will need to create a new connection for each packet of data?

HTTP requests can last for an arbitrarily long period of time, just as for raw TCP sockets. A GET request can last for hours if necessary. You need not create a new connection for each request — take a look at the Connection: Keep-Alive HTTP header.

Or is there a way to send server responses in chunks, over a single connection?

If you don't know the length of the response you can either omit a Content-Length header or, preferably, use the Transfer-Encoding: chunked HTTP header.

In what ways can an ISP proxy mess with the data, or the headers? For example, a proxy can sometimes keep a connection alive, even if the server closes it.

ISPs don't tend to reveal the changes they make to HTTP responses. If you are concerned about this a simple solution would be to encrypt the data and specify a Content-Encoding HTTP header. This would require you to control both the HTTP client and server.

这篇关于通过http发送二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆