[英]Add headers to request wrapped by ClientResource in Restlet
How do I add my own headers to a request wrapped by ClientResource
in Restlet? 如何将自己的标头添加到Restlet中ClientResource
包装的请求中? For example, I've read that you can use the following when working directly with Client
: 例如,我已经读过直接使用Client
时可以使用以下内容:
Form headers = (Form) request.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
if (headers == null) {
headers = new Form();
request.getAttributes().put("org.restlet.http.headers", responseHeaders);
}
headers.add("X-Some-Header", "the value");
However, I am basically following the code provided in their tutorial and I do not know which member of ClientResource
should be accessed to set headers: 但是,我基本上遵循他们的教程中提供的代码,我不知道应该访问哪个ClientResource
成员来设置标头:
ClientResource clientResource = new ClientResource("http://webserviceurl");
MyClassResource classResource = clientResource.wrap(classResource.class);
MyClass class;
try { class = resource.retrieve(); } catch (Exception e) { System.out.println("fail."); }
What can I do to modify retrieve()
to add some headers? 我该怎么做才能修改retrieve()
来添加一些标题?
The ClientResource method has a getRequestAttributes method which is a shortcut for: getRequest().getAttributes()
. ClientResource方法有一个getRequestAttributes方法,它是一个快捷方式: getRequest().getAttributes()
。
So you can use it in order to specify your custom headers for the request, as described below: 因此,您可以使用它来为请求指定自定义标头,如下所述:
ClientResource cr = new ClientResource("...");
Series<Header> headers = cr.getRequestAttributes().get(
"org.restlet.http.headers");
headers.set("<header-name>", "<header-value>");
Be aware that most of headers are managed by Restlet by default. 请注意,默认情况下,大多数标头都由Restlet管理。 To see which headers are supported, have a look at the HeaderUtils class: https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/engine/header/HeaderUtils.java . 要查看支持哪些标头,请查看HeaderUtils类: https : //github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/engine/header /HeaderUtils.java 。
Edited 编辑
With latest versions of Restlet (2.3), a method getHeaders
was added: 使用最新版本的Restlet(2.3),添加了一个方法getHeaders
:
ClientResource cr = new ClientResource("...");
Series<Header> headers = cr.getHeaders();
headers.set("<header-name>", "<header-value>");
This corresponds to custom headers. 这对应于自定义标头。
Hope it will help you. 希望它会对你有所帮助。 Thierry 蒂埃里
If you're using restlet 2.0.x (the latest stable version), you need to do this: 如果您使用的是restlet 2.0.x(最新的稳定版),则需要执行以下操作:
ClientResource resource = new ClientResource(yourUrl);
Form headers = (Form)resource.getRequestAttributes().get("org.restlet.http.headers");
if (headers == null) {
headers = new Form();
resource.getRequestAttributes().put("org.restlet.http.headers", headers);
}
headers.add("yourHeaderName", yourHeaderValue);
resource.get();
Response response = resource.getResponse();
String text = response.getEntity().getText();
String status = response.getStatus().toString();
This worked for me, so I'm sharing this. 这对我有用,所以我分享了这个。
ClientResource client = new ClientResource(uri);
Series<Header> headerValue = new Series<>(Header.class);
Request request = client.getRequest();
headerValue.add("header name", "header value");
request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, headerValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.