HttpClient HttpPost导致HTTP 400响应

时间:2015-04-21 02:24:28

标签: java http soap http-post

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

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

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

[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());

2 个答案:

答案 0 :(得分:0)

问题可能是高于/超出TCP / IP级别的任何问题&#34; connect&#34;。请看这里的几个例子:

强烈建议:

  • 获取SoapUI,如果您还没有。创建一个虚拟SOAP响应,将您的客户端指向SoapUI,并验证请求是否正常(听起来像是)。然后创建一个虚拟SOAP请求,并在服务器上指向SoapUI。看看你是否可以确切地确定&#34;出了什么问题&#34;。

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

  • 打开服务器上的完整日志记录,以便更好地了解&#34;出了什么问题&#34;。

问:您在客户端上使用了哪种语言和SOAP库?在服务器上?

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

答案 1 :(得分:0)

感谢您仔细研究并花时间回复。

问题比我想象的要简单。该服务需要两个字符串。我发送的其中一个字符串是XML格式,这使服务混乱。一旦我编码了字符串,服务器就能够接收并解析请求。