【问题标题】:How to add header for http request如何为http请求添加标头
【发布时间】:2010-10-22 04:49:19
【问题描述】:

我是 Restlet 开发的新手,正在尝试添加标头来执行 HTTP 请求。我尝试了以下代码,但得到“400 bad request, header is not valid”

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

【问题讨论】:

    标签: http header restlet


    【解决方案1】:

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

    ClientID 和 DeviceID 似乎不允许使用标头。如果你想要自定义标题,你应该在它们前面加上“X-”。

    【讨论】:

      【解决方案2】:

      尝试使用

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

      尽管可能还有其他问题(myCT 内容类型?)。也从未使用过 ClientID 和 DeviceID 标头...但我是 PHP 人 :)

      【讨论】:

      • 服务器要求所有这 4 个字段都包含在标头中。所以 clientID 和 DeviceID 必须在那里,我认为......
      • 好的,但是你打错了,不是 Centent-Type:,是 Content-Type,第二个 header。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2017-02-15
      • 2018-05-04
      • 2021-02-10
      相关资源
      最近更新 更多