Java Rest Call-单个会话中的多个呼叫

时间:2014-12-10 13:08:08

标签: java rest

我必须打电话给第三方网站。但他们需要登录密码。

我有一个resturl登录到该系统,另一个用于获取详细信息,但如何在单个会话中执行这两个调用我不知道。

以下是我提出休息请求的代码

private static void restGetResponse(final String url, final Map<String, String> requestParams,
            final boolean isWriteToString, final StringWriter stringWriter, final OutputStream outputStream)
            throws ClientProtocolException, IOException {
        HttpGet httpGet = new HttpGet(getURLWithParams(url, requestParams));
        // Get hold of the response entity
        HttpEntity entity = getHTTPResponse(httpGet);
        writeHTTPResponse(entity, isWriteToString, stringWriter, outputStream);
    }

    private static HttpEntity getHTTPResponse(final HttpUriRequest httpUriRequest) throws ClientProtocolException,
            IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        addProxy(client);
        HttpResponse response = client.execute(httpUriRequest);
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        return entity;
    }

    private static void writeHTTPResponse(final HttpEntity entity, final boolean isWriteToString,
            final StringWriter stringWriter, final OutputStream outputStream) throws IllegalStateException, IOException {
        // If the response does not enclose an entity, there is no need to bother about connection release
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
                if (isWriteToString) {
                    IOUtil.writeToWriter(inputStream, stringWriter);
                } else {
                    IOUtil.writeToOutputStream(inputStream, outputStream);
                }
            } finally {
                IOUtil.close(inputStream);
            }
        }
    }

请建议我在多次休息电话中进行sesseion或共享cookie。

1 个答案:

答案 0 :(得分:0)

我建议使用http客户端库。我正在使用Apache HTTP Client。它充满了tutorial about cookie handling。有关客户的问题可能已在此处得到解答,请参阅https://stackoverflow.com/questions/tagged/apache-commons-httpclient