0

我正在使用 Webharvest 从网站下载文件并取其原始名称。

我正在使用的 Java 代码是:

import org.apache.commons.httpclient.Header;
            import org.apache.commons.httpclient.HttpClient;
            import org.apache.commons.httpclient.HttpStatus;
            import org.apache.commons.httpclient.Header;
            import org.apache.commons.httpclient.methods.GetMethod; 

            HttpClient client = new HttpClient();

            BufferedReader br = null;
            StringBuffer result = new StringBuffer();
            String attachName;

            GetMethod method = new GetMethod(attachmentLink.toString());

            int returnCode; 
            returnCode = client.executeMethod(method);
            Header[] headers = method.getResponseHeader("Content-Disposition");
            attachName = headers[0].getValue();
            attachName = new String(attachName.getBytes());

webharvest 的结果是:

附件; filename="Resoluci�n sobre Mesas de Contrataci�n.pdf"

我不能让它接受这封信

Ø

在将标头 Content-Disposition 的值转换为变量 attachName 后,我也尝试对其进行解码,但没有运气:

String attachNamef = URLEncoder.encode(attachName, "ISO-8859-1"); 
                      attachNamef = URLEncoder.decode(attachNamef, "UTF-8");

我能够确定响应字符集是:ISO-8859-1

method.getResponseCharSet()

PS 当我在 Firefox Firebug 中看到标题时 - 值没问题:Content-Disposition

附件; filename="Resolución sobre Mesas de Contratación.pdf"

4

1 回答 1

1

Apache HttpClient 不支持 HTTP 标头中的非 ascii 字符。取自文档

HTTP 请求或响应的标头必须采用 US-ASCII 格式。不能在请求或响应的标头中使用非 US-ASCII 字符。但是,通常这不是问题,因为 HTTP 标头旨在促进数据传输,而不是实际传输数据本身。然而,cookie 是一个例外。由于 cookie 作为 HTTP 标头传输,因此它们仅限于 US-ASCII 字符集。有关更多信息,请参阅 Cookie 指南。

于 2017-01-16T16:01:45.380 回答