相关疑难解决方法(0)

URLConnection不遵循重定向

我无法理解为什么Java HttpURLConnection不遵循重定向.我使用以下代码来获取此页面:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;

public class Tester {

    public static void main(String argv[]) throws Exception{
        InputStream is = null;

        try {
            String httpUrl = "http://httpstat.us/301";
            URL resourceUrl = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection)resourceUrl.openConnection();
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(15000);
            conn.connect();
            is = conn.getInputStream();
            System.out.println("Original URL: "+httpUrl);
            System.out.println("Connected to: "+conn.getURL());
            System.out.println("HTTP response code received: "+conn.getResponseCode());
            System.out.println("HTTP response message received: "+conn.getResponseMessage());
       } finally {
            if (is != null) is.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而且,我收到以下回复(看起来绝对正确!):

Original URL: http://httpstat.us/301
Connected to: …

java https redirect httpurlconnection http-redirect

92
推荐指数
5
解决办法
8万
查看次数

Spring RestTemplate重定向302

我正在尝试使用spring rest模板来发布登录请求.

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

LinkedMultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
mvm.add("LoginForm_Login", "login");
mvm.add("LoginForm_Password", "password");

ResponseEntity<String> result = restTemplate.exchange(uriDWLogin, HttpMethod.POST, requestEntity, String.class);
Run Code Online (Sandbox Code Playgroud)

我的ResponseEntity状态是302,我想跟随此请求获取正文响应,因为我没有获得此请求的正文.

18:59:59.170 MAIN [http-nio-8080-exec-83] DEBUG c.d.s.c.DemandwareCtlr - loginToSandbox - StatusResponse - 302
18:59:59.170 MAIN [http-nio-8080-exec-83] DEBUG c.d.s.c.DemandwareCtlr - loginToSandbox - BodyResponse - 
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

rest redirect spring response http-status-code-302

8
推荐指数
1
解决办法
2万
查看次数

即使使用 GET,RestTemplate 也不遵循重定向

所以,

我正在使用 RestTemplate 访问某个页面,这将需要重定向(重定向到 oauth 2 提供程序,但这可能无关紧要)。

我正在做:

ResponseEntity<String> forEntity = oAuth2RestTemplate.getForEntity(url, String.class, Collections.emptyMap());
Run Code Online (Sandbox Code Playgroud)

使用标准OAuth2RestTemplate,我还没有对这个 bean 进行任何自定义创建,所以它只是从 spring 注入的。

作为响应,我得到ResponseEntitylocation标头和 302 状态,但没有遵循。从许多其他来源判断,如果我所做的只是GET- 类似的问题:Follow 302 redirect using Spring restTemplate?

这不是一个案例,没有重定向发生,我错过了什么吗?我是否必须手动抓取它location 并手动重定向到它?

更新:

好的,作为它的 OAuth2RestTemplate,它使用 OAuth2AccessTokenSupport 作为请求工厂。此类将工厂配置为遵循重定向。可惜太难找了。

但是使用也= new RestTemplate()无济于事(注意 new 关键字,如果您要自动装配它,那么 spring 将自动装配 oauth2RestTemplate )。

spring resttemplate spring-oauth2

5
推荐指数
0
解决办法
3733
查看次数