gpt4 book ai didi

java - 使用Java获取响应头,编码问题

转载 作者:行者123 更新时间:2023-12-01 09:00:52 28 4
gpt4 key购买 nike

我正在使用 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 中的结果是:

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

我无法让它接受这封信

ó

将 header Content-Disposition 的值获取到变量 AttachName 后,我也尝试对其进行解码,但没有成功:

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

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

method.getResponseCharSet()

附注当我在 Firefox Firebug 中看到 header 时 - 值正常:内容处置

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

最佳答案

Apache HttpClient 不支持 HTTP header 中的非 ASCII 字符。 Taken from documentation :

The headers of a HTTP request or response must be in US-ASCII format. It is not possible to use non US-ASCII characters in the header of a request or response. Generally this is not an issue however, because the HTTP headers are designed to facilite the transfer of data rather than to actually transfer the data itself. One exception however are cookies. Since cookies are transfered as HTTP Headers they are confined to the US-ASCII character set. See the Cookie Guide for more information.

关于java - 使用Java获取响应头,编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41680150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com