【问题标题】:How do you send a RESTLET POST request without the optional parameter "; CHARSET=UTF-8" in the HTTP header?如何在 HTTP 标头中没有可选参数“;CHARSET=UTF-8”的情况下发送 RESTLET POST 请求?
【发布时间】:2014-12-30 21:53:07
【问题描述】:

如何在 HTTP 标头中发送可选参数“;CHARSET=UTF-8”的 RESTLET POST 请求?

当我在 UNIX 中使用 WGET 为我的服务尝试这个概念证明时,我可以像这样在 HTTP 标头中指定可选的 Content-type: ...:

指定编码为 UTF-8:

wget --header="Content-Type: application/x-www-form-urlencoded; charset=UTF-8"  --post-file=input_file.xml https://localhost/myservice

省略编码:

wget --header="Content-Type: application/x-www-form-urlencoded"  --post-file=input_file.xml https://localhost/myservice

当我在 RESTLET 中做这样的事情时

Form form = new Form();

String accessToken = "Moroni123";

form.set("access_token", accessToken);

如果我这样做是为了查看表示:

Representation rep = form.getWebRepresentation();

我使用 rep.toString() 方法看到了这一点:

[application/x-www-form-urlencoded,UTF-8]

有没有办法让它看起来像这样? SoapUI 在没有“UTF-8”的情况下发送它,我如何在 RESTLET 中做到这一点?

[application/x-www-form-urlencoded]

【问题讨论】:

  • 你能给我你使用的Restlet版本吗?谢谢!

标签: utf-8 soapui soap-client restlet


【解决方案1】:

事实上,这个提示对应于 Restlet 表示中的字符集。您可以简单地将其设置为null,在表示上使用setCharacterSet 方法,并且标题Content-Type 中将不再有UTF-8。以下代码对此进行了说明:

Form form = new Form();
String accessToken = "Moroni123";
form.set("access_token", accessToken);

Representation repr = form.getWebRepresentation();
MediaType mediaType = repr.getMediaType();
// corresponds to application/x-www-form-urlencoded
CharacterSet characterSet = repr.getCharacterSet();
// corresponds to UTF-8

rep.setCharacterSet(null);

ClientResource cr = new ClientResource("http://...");
cr.post(repr);

我们将有以下要求:

POST / HTTP/1.1
Date: Thu, 19 Feb 2015 15:58:06 GMT
Content-Type: application/x-www-form-urlencoded
Accept: */*
User-Agent: Restlet-Framework/2.3.0
Cache-Control: no-cache
Pragma: no-cache
Host: 127.0.0.1:8181
Connection: keep-alive
Content-Length: 22

access_token=Moroni123

没有这个,标题Content-type的内容是:

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

希望对你有帮助, 蒂埃里

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2010-12-23
    相关资源
    最近更新 更多