---
title: "restlet中获取头信息"
author: "chigooo"
date: "2012-12-11T10:59:21Z"
tags: []
categories: ["企业架构"]
slug: "restlet"
draft: false
lastmod: "2012-12-11T11:00:02Z"
id: 1745648
url: "https://chigooo.iteye.com/blog/1745648"
---

restlet框架中，获取http头信息的内容：
1. 在server端，resource中，可以写个方法，直接调用。

```java
	protected Map<String,String> getHeader() {
		Form headForm = (Form) getRequest().getAttributes().get(
				"org.restlet.http.headers");
		return headForm.getValuesMap();
	}

```


2. 在client端，可以通过
```java
Form headForm = (Form)client.getResponse().getAttributes().get("org.restlet.http.headers");
```


现在是我在做url重定向时的一个测试用例,模拟post请求：

```java

client = new ClientResource("http://xxx");
String xml = "<?xml version="+ "\"1.0\"" + " encoding=" + "\"UTF-8\"" + "?>" +
		"<request><operation>getrequest</operation>"+</request>";
StringRepresentation representation=new StringRepresentation(xml, MediaType.TEXT_XML);
client.post(representation);  
Form headForm = (Form)client.getResponse().getAttributes().get("org.restlet.http.headers");
System.out.println(headForm.getValues("Location"));

```

最后输出的是301返回页面头信息中，重定向url值。