Java Apache httpdelete - 400错误

时间:2016-10-13 10:53:15

标签: java apache apache-httpclient-4.x http-delete

我很难确定BAD响应来自哪里,我已经在下面的Java代码中执行了请求,但也从SOAP UI中执行了DELETE REST调用,我获得了成功的200响应。 (电话相同)

以下代码旨在登录&通过设置cookie来注销会话 - 然后将它们作为标题传递给删除调用

Java代码

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class testing {

    public static void main(String[] args) throws Exception {

        HttpResponse httpResponse = null;

        CloseableHttpClient httpclient = HttpClients.createDefault();

        RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
                .setProxy(new HttpHost("xxx.xxx.xxx.xxx", 8080))
                .build();

        HttpHost target = new HttpHost("https://xxxxxxxxx.xxxxxxxxx.com");

        HttpPost post = new HttpPost("https://xxxxxxxxx.xxxxxxxxx.com/authentications/emails");

        post.setConfig(requestConfig);

        post.addHeader("Accept-Language","en-GB");
        post.addHeader("X-ApiKey","xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx");
        post.addHeader("Content-Type", "application/json");


        StringEntity input = new StringEntity("{\"Password\":\"xxxxxxxxx\",\"Email\":\"xxxxxxxxx.xxxxxxxxx@xxxxxxxxx.com\"}");
        input.setContentType("application/json");
        post.setEntity(input);

        httpResponse = httpclient.execute(target, post);

        Header[] headers = httpResponse.getAllHeaders();

        for (Header header: headers)
        {
            System.out.println(header.getName() + ": " + header.getValue());
        }

        String[] ss_id_list = headers[10].getValue().split(";");
        String ss_id = ss_id_list[0];
        String[] ss_pid_list = headers[11].getValue().split(";");
        String ss_pid = ss_pid_list[0];

        HttpDelete call = new HttpDelete("https://xxxxxxxxx-xxxxxxxxx.xxxxxxxxx.com/authentications/current");

        call.setConfig(requestConfig);

        call.setHeader("Accept-Language","en-GB");
        call.setHeader("X-ApiKey","xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx");
        call.setHeader("Cookie", ss_id +";"+ ss_pid +"; ss-opt=temp; X-UAId=xxxxxxxxx;");
        call.setHeader("Content-Type", "application/x-www-form-urlencoded");

        Header[] rheaders = call.getAllHeaders();

        System.out.println(call.toString());
        for (Header rheader : rheaders) {
            System.out.println(rheader.getName() + ": " + rheader.getValue());
        }

        httpResponse = httpclient.execute(target, call);

        BufferedReader br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));

        StringBuilder content = new StringBuilder();
        String line;
        while (null != (line = br.readLine())) {
            content.append(line);
        }

        System.out.println(httpResponse.getStatusLine());

        httpclient.close();

    }





}

Java请求&响应

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Type: application/json; charset=utf-8
Date: Thu, 13 Oct 2016 10:33:21 GMT
X-Frame-Options: SAMEORIGIN
X-Request-Identifier: xxxxxxxxx
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Set-Cookie: ss-id=xxxxxxxxx; path=/; HttpOnly
Set-Cookie: ss-pid=xxxxxxxxx; expires=Mon, 13-Oct-2036 10:33:21 GMT; path=/; HttpOnly
Set-Cookie: ss-opt=temp; expires=Mon, 13-Oct-2036 10:33:21 GMT; path=/; HttpOnly
Set-Cookie: X-UAId=xxxxxxxxx; expires=Mon, 13-Oct-2036 10:33:21 GMT; path=/; HttpOnly


DELETE https://xxxxxxxxx.xxxxxxxxx.com/authentications/current HTTP/1.1
Accept-Language: en-GB
X-ApiKey: xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx
Cookie: ss-id=xxxxxxxxx;ss-pid=xxxxxxxxx; ss-opt=temp; X-UAId=xxxxxxxxx;
Content-Type: application/x-www-form-urlencoded
HTTP/1.1 400 Bad Request

SOAP UI请求

DELETE https://xxxxxxxxx.xxxxxxxxx.com/authentications/current HTTP/1.1
Accept-Encoding: gzip,deflate
X-ApiKey: xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx
Accept-Language: en-GB
Cookie: ss-id=xxxxxxxxx;ss-pid=xxxxxxxxx; ss-opt=temp; X-UAId=xxxxxxxxx;
Content-Type: text/html
Content-Length: 0
Host: xxxxxxxxx.xxxxxxxxx.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

SOAP UI响应

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Thu, 13 Oct 2016 10:15:29 GMT
X-Frame-Options: SAMEORIGIN
X-Request-Identifier: xxxxxxxxx
Content-Length: 59
Connection: keep-alive

{"IsNewUser":false,"ResponseStatus":{}}

我为代码转储而道歉响应是我遗漏的Java代码中有一些明显的东西。 Java解决方案和SOAP UI正在为此请求使用Same PROXY服务器。

1 个答案:

答案 0 :(得分:0)

事实证明,违规行是:

<li820>
<data><celltemp>5.1120729e1</celltemp><cellpres>9.7705745e1</cellpres><co2>7.7808494e2</co2><co2abs>5.0983281e-2</co2abs><ivolt>1.1380004e1</ivolt><raw>2726238,1977386</raw></data>
<data><celltemp>5.1120729e1</celltemp><cellpres>9.7705745e1</cellpres><co2>7.7808494e2</co2><co2abs>5.0983281e-2</co2abs><ivolt>1.1380004e1</ivolt><raw>2726238,1977386</raw></data>
</li820>

然而,使用HTTPS连接执行此操作会导致找不到可信证书的异常错误。

我找到了很多关于实施信任管理器的答案。主机名验证程序,但不适用于Apache httpclient(适用于v4.3)

HttpHost target = new HttpHost("https://xxxxxxxxx.xxxxxxxxx.com");