泽西岛未关注302重定向 [英] Jersey is not following 302 redirects

查看:121
本文介绍了泽西岛未关注302重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到服务器,该服务器首先进入auth登录页面,然后重定向.

I am connecting to a server which first goes to an auth login page and then redirects.

ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
Client client = Client.create(config);
client.setFollowRedirects(true);
WebResource service = client.resource(UriBuilder.fromUri(url).build());

String output = service.path(resource)
        .path(model)
        .path(id)
        .accept(MediaType.APPLICATION_JSON)
        .get(String.class);

这引发异常:

线程"ThreadJob"中的异常 com.sun.jersey.api.client.UniformInterfaceException:GET https://url 返回的响应状态为302找到 在com.sun.jersey.api.client.WebResource.handle(WebResource.java:688) 位于com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74) 在com.sun.jersey.api.client.WebResource $ Builder.get(WebResource.java:509)

Exception in thread "ThreadJob" com.sun.jersey.api.client.UniformInterfaceException: GET https://url returned a response status of 302 Found at com.sun.jersey.api.client.WebResource.handle(WebResource.java:688) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)

Jersey版本1.19

Jersey version 1.19

推荐答案

我发现球衣经常很烂.您几乎会认为他们意味着您要做这种事情,并且他们添加了"followRedirect"选项以使用户感到困惑.

I find that often Jersey sucks. You would almost think they meant you to do this sort of thing and they add the "followRedirect" options to simply confuse the user.

import static org.junit.Assert.*;

import java.util.logging.Logger;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.junit.Test;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;


public class TestJerseyRedirect {

    @Test
    public void test() {
        ClientConfig config = new DefaultClientConfig();
        config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
        com.sun.jersey.api.client.Client client = Client.create(config);
        client.setFollowRedirects(true);

        LoggingFilter logging = new LoggingFilter(Logger.getAnonymousLogger());


        WebResource service = client.resource(UriBuilder.fromUri("http://mail.google.com").build());
        service.addFilter(logging);
        try {
            String output = service.path("mail")
                .accept(MediaType.TEXT_HTML)
                .get(String.class);

            System.out.println(output);

        } catch (UniformInterfaceException e) {

            if (e.getResponse().getStatus() == 302) {
                String location = e.getResponse().getHeaders().getFirst("Location");

                WebResource service2 = client.resource(UriBuilder.fromUri(location).build());
                service2.addFilter(logging);
                String output2 = service2
                        .accept(MediaType.TEXT_HTML)
                        .get(String.class);

                    System.out.println(output2);

            }
            else {
                e.printStackTrace();
                throw e;
            }
        }

    }
}

这将起作用,但显然不是正确的方法.这种情况并没有那么糟,但是当存在多个重定向等时,可能会给您带来麻烦.

which will work but is clearly not the right way to do it. This is less worse, but could give you trouble when there are, among other things, multiple redirects and the like.

@Test
public void testGoodWay() throws Exception {
    ClientConfig config = new DefaultClientConfig();
    config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
    com.sun.jersey.api.client.Client client = Client.create(config);
    client.setFollowRedirects(true);

    LoggingFilter logging = new LoggingFilter(Logger.getAnonymousLogger());



    WebResource service = client.resource(UriBuilder.fromUri("http://mail.google.com").build());
    service.addFilter(logging);
    service.addFilter(new RedirectFilter());

    try {
        String output = service.path("mail")
            .accept(MediaType.TEXT_HTML)
            .get(String.class);

        System.out.println(output);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

class RedirectFilter extends ClientFilter {

    @Override
    public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
        ClientHandler ch = getNext();
        ClientResponse resp = ch.handle(cr);

        if (resp.getClientResponseStatus().getFamily() != Response.Status.Family.REDIRECTION) {
            return resp;
        }
        else {
            // try location
            String redirectTarget = resp.getHeaders().getFirst("Location");
            cr.setURI(UriBuilder.fromUri(redirectTarget).build());
            return ch.handle(cr);
        }

    }

}

这篇关于泽西岛未关注302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆