试图复制与JSoup一个成功的POST请求 - 发布到服务器上的数据没有得到德codeD器上、数据、JSoup、POST

2023-09-13 01:39:04 作者:打小就贱

HTTP请求头:

 主持人:www.mysite.com
内容类型:应用程序/ x-WWW的形式urlen codeD
饼干:bbuserid = XXX; bbpassword = YYY; bbsessionhash = ZZZ
内容长度:252
 

HTTP请求正文:

message=%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC&securitytoken=XXX&do=postreply&t=483553

做工精细!发送到服务器的数据获取去codeD上的另一端,用户看到原单的消息是äöõüäöõüäöõüäöõü

超文本传输协议HTTP总结 简单的HTTP服务器

现在让我们尝试实现这个excact例如用JSoup:

  //请求主体
地图<字符串,字符串>数据映射=新的HashMap<字符串,字符串>();
datamap.put(Session.SESSION_SECURITYTOKEN,XXX);
datamap.put(信息,URLEn coder.en code(finalText,ISO-8859-1));
datamap.put(做,postreply);
datamap.put(T,483553);

//使一个职位
Jsoup.connect(URL)
.header(内容类型,应用程序/ x-WWW的形式urlen codeD)
.timeout(10000)
.cookie(Session.COOKIE_HASH_KEY,session.bbsessionhash)
.cookie(Session.COOKIE_PASSWORD_KEY,session.bbpassword)
.cookie(Session.COOKIE_USERID_KEY,session.bbuserid)
.DATA(数据映射)。员额();
 

我的消息被公布,但它不是去codeD服务器。因此,当用户查看邮件,他/她认为:%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC

注:我做从Android的POST请求,并发布数据vBulletin论坛软件(重播线程)

的问题::当我发送消息JSoup,服务器看到它像一个纯文本而不是连接codeD文本。 我怎样才能让服务器明白该消息参数保存EN codeD文本,而不是纯文本?

解决方案

Jsoup使用UTF-8在默认情况下的URL-CN code中的查询字符串。在当前的API版本,你不能没有重建源改变它(这是 org.jsoup.helper.DataUtil#defaultCharset 常数在 org.jsoup.helper.HttpConnection 类)。最好的,你可以做的是发布问题报告要求的能力,以preSET的字符集事前。

在那之前,你可以使用 HttpClient的 URLConnection的 ,而不是允许进行更精细地控制发送HTTP请求。你终于可以养活自己作为一个的InputStream Jsoup#解析()法的回应。

更新:如果目标网站的支持,您可以尝试显式指定的内容类型请求头中的客户端的使用的字符集:

  .header(内容类型,应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8)
 

请注意,您应该不会使用 URLEn codeR#恩code()自己;让Jsoup完成其工作。

Http request header:

Host: www.mysite.com
Content-Type: application/x-www-form-urlencoded
Cookie: bbuserid=XXX; bbpassword=YYY; bbsessionhash=ZZZ
Content-Length: 252

Http request body:

message=%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC&securitytoken=XXX&do=postreply&t=483553

Working fine! Data posted to server gets decoded on the other end and user sees orginal message which is äöõüäöõüäöõüäöõü.

Now lets try to implement this excact example with JSoup:

//request body 
Map<String, String> datamap = new HashMap<String, String>();
datamap.put(Session.SESSION_SECURITYTOKEN,"XXX");
datamap.put("message", URLEncoder.encode(finalText, "ISO-8859-1"));
datamap.put("do", "postreply");
datamap.put("t", "483553");

//make a post
Jsoup.connect(url)
.header("Content-Type","application/x-www-form-urlencoded")
.timeout(10000)
.cookie(Session.COOKIE_HASH_KEY,session.bbsessionhash)
.cookie(Session.COOKIE_PASSWORD_KEY,session.bbpassword)
.cookie(Session.COOKIE_USERID_KEY,session.bbuserid)
.data(datamap).post();

My message gets posted BUT it is not decoded by the server. So when user views the message he/she sees: %E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC

Note: I am doing the post request from Android and posting data to vBulletin forum software (replay to thread).

The problem: When I send the message with JSoup, server sees it like a plain text not a encoded text. How can I make the server to understand that the message parameter holds encoded text, not plain text?

解决方案

Jsoup uses UTF-8 by default to URL-encode the query string. With the current API version, you cannot change it without rebuilding the source (it's the org.jsoup.helper.DataUtil#defaultCharset constant which is been used in org.jsoup.helper.HttpConnection class). Best what you can do is to post an issue report requesting the ability to preset the charset beforehand.

Until then, you could use HttpClient or URLConnection instead which allows for a more finer grained control over sending HTTP requests. You could finally feed its response as an InputStream to Jsoup#parse() method.

Update: if the target website supports it, you could try explicitly specifying the client's used charset in the Content-Type request header:

.header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")

Note that you should not use URLEncoder#encode() yourself; let Jsoup do its job.