gpt4 book ai didi

java - Jersey 不遵循 302 重定向

转载 作者:搜寻专家 更新时间:2023-11-01 01:42:18 25 4
gpt4 key购买 nike

我正在连接到一个服务器,它首先进入一个授权登录页面,然后重定向。

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

这是抛出异常:

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 经常很糟糕。你几乎会认为他们是想让你做这种事情,他们添加了“followRedirect”选项来简单地混淆用户。

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;
}
}

}
}

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

@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);
}

}

}

关于java - Jersey 不遵循 302 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29955951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com