0

我无法通过我敲开的一个小球衣客户端测试连接到我在远程机器上运行的服务。

我可以像这样使用 cURL 成功连接和发布:

curl -X POST -T test.txt -H "Content-Type: application/octet-stream" http://remote-machine:11181/data?start=123

我设置的服务很remote-machine乐意接受这一点并按预期工作,但是当通过我的球衣客户端实现处理完全相同的 URI 时,我收到以下异常:

com.sun.jersey.api.client.UniformInterfaceException: POST http://remote-machine:11181/data?start=123 returned a response status of 502 Bad Gateway
    at com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:707)
    at com.sun.jersey.api.client.WebResource.access$400(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:553)

我的泽西岛客户如下:

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println("Usage: " + TestJerseyClient.class.getName() + " <uri> <input file>");
        System.exit(-1);
    }

    File file = new File(args[1]);
    if (!file.exists()) {
        System.err.println("File not found: " + file.getAbsolutePath());
        System.exit(-2);
    }

    URI uri = URI.create(args[0]);
    Client client = new Client();
    client.setChunkedEncodingSize(16 * 1024);

    FileInputStream stream = new FileInputStream(file);
    try {
        System.out.println("Sending " + file.getPath());
        client.resource(uri).type(MediaType.APPLICATION_OCTET_STREAM).post(stream);
        System.out.println("Done");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Closeables.closeQuietly(stream);
    }
}

关于为什么我的球衣客户端在获得与 cURL 相同的 URI 后,为什么会收到这些 502 Bad Gateway 错误的任何帮助,我们将不胜感激。

(我想这可能是一个代理问题,但至于让球衣像 cURL 那样被他们选中,我不知道。)

4

1 回答 1

0

我设法通过使用ApacheHttpClient而不是标准 Jersey来解决这个问题Client。不完全确定表面之下的差异,但似乎它似乎ApacheHttpClient比标准的 Jersey 客户端更好地接受任何配置和代理,这很可能是它被创建的原因之一。

我必须改变的是:

Client client = new Client();

Client client = new ApacheHttpClient();

当然,相应地更新我的依赖项等。

于 2013-04-01T17:01:48.597 回答