如何为http请求添加标头

问题描述:

我是Restlet开发的新手,试图添加标头来执行HTTP请求。我想下面的代码,却得到了“400错误的请求,标题是无效”如何为http请求添加标头

String url = "http://xxxxx"; 
Client c = new Client(Protocol.HTTP); 

Request request = new Request(Method.GET, url); 
HashMap attributes = new HashMap(); 

attributes.put = ("DeviceID", "myDeviceID"); 
attributes.put = ("Centent-Type", "myCT"); 
attributes.put = ("User-Agent", "my user agent"); 
attributes.put = ("ClientID", "myCid"); 

request.setAttributes(attributes); 
Response r =c.handle(request); 

我使用的Restlet 2.0。 请帮忙。任何示例代码都会很有帮助。提前致谢。 KC使用

attributes.put = ("Content-Type", "myCT"); 

Altough可能还有其他的问题,以及

尝试(myCT内容类型?)。没用过的ClientID和的DeviceID头也......但我是一个PHP的家伙:)

+0

服务器需要包含在标题中的所有这4个字段。所以clientID和DeviceID必须在那里,我认为... – androidkc 2010-10-22 14:13:11

+0

好的,但你有一个错字,它不是Centent-Type:,它是Content-Type,第二个头文件。 – Claudiu 2010-10-22 14:38:16

HTTP协议有允许标题的列表:http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

客户端ID和的DeviceID似乎并没有被允许的头。如果你想自定义标题,你应该用“X-”作为它们的前缀。