如何发送RESTLET POST请求不带可选参数&#34 ;; CHARSET = UTF-8"在HTTP标题?
当我在UNIX中使用WGET为我的服务尝试这个概念证明时,我可以在HTTP头中指定可选的Content-type:...,如下所示:
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]
答案 0 :(得分:0)
实际上,此提示对应于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
希望它有所帮助, 亨利