在Http POST标头中发送非ASCII文本 [英] Sending non-ASCII text in Http POST header

查看:119
本文介绍了在Http POST标头中发送非ASCII文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件作为八位字节流发送到服务器,我需要在标题中指定文件名:

I am sending a file to a server as an octet-stream, and I need to specify the filename in the header:

String filename = "«úü¡»¿.doc"
URL url = new URL("http://www.myurl.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.addRequestProperty("Accept", "application/json; charset=UTF-8");
conn.addRequestProperty("Content-Type", "application/octet-stream; charset=UTF-8");
conn.addRequestProperty("Filename", filename);
// do more stuff here

问题是,我需要的一些文件send包含包含非ASCII字符的文件名。我已经读过你不能在HTTP标题中发送非ASCII文本。

The problem is, some of the files I need to send have filenames that contain non-ASCII characters. I have read that you cannot send non-ASCII text in an HTTP header.

我的问题是:


  1. 你能在HTTP标题中发送非ASCII文本吗?

  2. 如果可以的话,你怎么做?当filename包含非ASCII文本时,上面的代码不起作用。服务器响应错误请求400。

  3. 如果你不能,通常的
    方法是什么来解决这个限制?


推荐答案

您不能在HTTP标头中使用非ASCII字符,请参阅RFC 2616.URI本身由RFC 2396标准化,不允许也是非ASCII的。 RFC说:

You cannot use non ASCII character in HTTP headers, see the RFC 2616. URI are themselves standardized by RFC 2396 and don't permit non-ASCII either. The RFC says :


URI语法的设计具有全局可转换性,是其主要关注点之一。
。 URI是一个非常
限制集的字符序列,即基本拉丁字母,数字,
和一些特殊字符的字母。

The URI syntax was designed with global transcribability as one of its main concerns. A URI is a sequence of characters from a very limited set, i.e. the letters of the basic Latin alphabet, digits, and a few special characters.

为了在URI中使用非ASCII字符,你需要使用%hexcode语法来转义它们(参见RFC 2396的第2节)。

In order to use non ASCII characters in URI you need to escape them using the %hexcode syntax (see section 2 of RFC 2396).

在Java中,您可以使用 java.net.URLEncoder 类来完成此操作。

In Java you can do this using the java.net.URLEncoder class.

这篇关于在Http POST标头中发送非ASCII文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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