java - Jersey 和自定义 Expect header

标签 java php apache http-headers jersey

首先是代码:

Client client = Client.create();

WebResource webResource = client
        .resource("[link]");

ClientResponse response = webResource
        .header("Expect", "X-API-Key: [api_key]")
        .accept(MediaType.APPLICATION_JSON_TYPE)
        .post(ClientResponse.class, json);

String output = response.getEntity(String.class);

我的输出是:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>417 Expectation Failed</title>
</head><body>
<h1>Expectation Failed</h1>
<p>The expectation given in the Expect request-header
   field could not be met by this server.
        The client sent<pre>
        Expect: X-API-Key: [api_key]
        </pre>
 </p><p>Only the 100-continue expectation is supported.</p>
 </body></html>

如果我使用curl 和php 一切正常。我已经在 Apache 中设置了:

<IfModule mod_headers.c>
    RequestHeader unset Expect early
</IfModule>

什么也没有。错误消失,但未设置 X-API-Key。

最佳答案

它有效!

关闭重定向:

client.setFollowRedirects(false);

并删除期望:

ClientResponse response = webResource
        .header("X-API-Key", config.getKey())
        .type("application/json")
        .post(ClientResponse.class, json);

干杯!

关于java - Jersey 和自定义 Expect header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23492218/

相关文章:

php - 一个查询php中的多个mysql INSERT语句

apache - 如果使用任何 IE 浏览器 - htaccess,则将站点的所有页面重定向到页面

php - 如何在 PHP 中检查不完整的 POST 请求

php - magento 大尺寸可下载产品无法下载

java - Spring进程自动运行

java - 构建后 JLine3 "Unable to create a system terminal"

php - 检查两个 MySQL 表中是否存在值

java - Kotlin + Spring AliasFor

java - 如何在 swt 中从父组合中删除子组合?

php - 多次mysql查询导致返回null