【问题标题】:HttpClient HttpPost results in HTTP 400 responseHttpClient HttpPost 导致 HTTP 400 响应
【发布时间】:2015-04-23 03:20:55
【问题描述】:

我正在编写一个 SOAP 客户端,使用 org.apache.http.impl.client.CloseableHttpClient 通过代理服务器对外部系统进行 SOAP 调用。

从日志文件看来,通过代理服务器的连接成功,但对外部系统端点的 POST 请求导致 HTTP 400 Bad Request 响应。

我无法弄清楚导致此错误的原因。任何人都可以看看,看看缺少什么?提前感谢您的帮助。

[4/20/15 20:37:04:850 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "POST /Staging/DataTransfer.asmx HTTP/1.1[\r][\n]"
[4/20/15 20:37:04:850 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "SOAPAction: http://data.types.services.ecomm.stores.com/TransferCart[\r][\n]"
[4/20/15 20:37:04:850 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "Content-Type: text/xml[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "Accept-Encoding: text/xml[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "Content-Length: 2022[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "Host: abc.test.com:443[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "Connection: Keep-Alive[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "User-Agent: Apache-HttpClient/4.4.1 (Java/1.6.0)[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "[\r][\n]"
[4/20/15 20:37:04:865 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 >> "TESTDATA"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "HTTP/1.1 400 Bad Request[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "via: IG Proxy[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "Date: Tue, 21 Apr 2015 00:37:01 GMT[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "Server: Microsoft-HTTPAPI/2.0[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "Connection: Keep-Alive[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "Content-Type: text/html; charset=us-ascii[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "Content-Length: 311[\r][\n]"
[4/20/15 20:37:05:099 EDT] 0000001c wire          1 org.apache.http.impl.conn.Wire wire http-outgoing-2 << "[\r][\n]"

当然 - 这是代码:

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(PROXY_HOST, PROXY_PORT),
                new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).build();

        HttpHost target = new HttpHost(ENDPOINT_HOST, 443, "https");
        HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);

        RequestConfig config = RequestConfig.custom()
        .setProxy(proxy)
        .build();

        HttpPost httppost = new HttpPost(ENDPOINT_API_URL);
        httppost.setConfig(config);
        httppost.setHeader("SOAPAction", SOAPAction);
        httppost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8;");
        httppost.setHeader("Accept-Encoding", "text/xml"); 
        httppost.setEntity(new StringEntity(postBody, "UTF-8"));

        //if(LOG.isLoggable(Level.FINE)) 
        LOG.info("Executing request " + httppost.getRequestLine() + " to " + target + " via " + proxy);

        CloseableHttpResponse response = httpclient.execute(target, httppost);

        LOG.info("CartTransfer HTTP Status=" + response.getStatusLine());

【问题讨论】:

  • 请向我们提供帮助您的方法。 TL;DR 发布您的代码。

标签: java http soap http-post


【解决方案1】:

问题可能只是高于/超出您的 TCP/IP 级别“连接”的任何内容。看看这里的几个例子:

强烈建议:

  • 如果您还没有,请获取SoapUI。创建一个虚拟 SOAP 响应,将您的客户端指向 SoapUI,并验证请求是否正常(听起来是这样)。然后创建一个虚拟 SOAP 请求,并将 SoapUI 指向您的服务器。看看你是否能准确地确定“出了什么问题”。

  • 如果您还没有,请获取Fiddler2。观察来回的流量(请求和响应)。

  • 在服务器上启用完整日志记录以更好地了解“出了什么问题”。

问:您在客户端上使用什么语言和 SOAP 库?在服务器上?

问:是否可以发布失败的客户端代码和服务器端代码的示例 sn-ps?

【讨论】:

    【解决方案2】:

    感谢您调查并花时间回复。

    问题比我预期的要简单。该服务需要两个字符串。我发送的字符串之一是 XML 格式,这使服务感到困惑。一旦我对字符串进行编码,服务器就能够接收和解析请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      相关资源
      最近更新 更多