java 重定向下载_Java 获取网络302重定向URL的方法

本文介绍了Java中获取网络302重定向URL的两种方法。方法1使用HttpURLConnection,方法2利用HttpClient和HttpClientContext。通过设置相应参数,阻止自动跟随重定向,并获取重定向地址。
摘要由CSDN通过智能技术生成

方法1:

import java.net.HttpURLConnection;

import java.net.URL;

import org.junit.Assert;

import org.junit.Test;

public class GetRedirectUrlTest {

@Test

public void test_getRedirectUrl() throws Exception {

String url="http://www.baidu.com/link?url=ByBJLpHsj5nXx6DESXbmMjIrU5W4Eh0yg5wCQpe3kCQMlJK_RJBmdEYGm0DDTCoTDGaz7rH80gxjvtvoqJuYxK";

String expectUrl="http://www.zhihu.com/question/20583607/answer/16597802";

String redictURL = getRedirectUrl(url);

Assert.assertEquals(expectUrl, redictURL);

}

/**

* 获取重定向地址

* @param path

* @return

* @throws Exception

*/

private String getRedirectUrl(String path) throws Exception {

HttpURLConnection conn = (HttpURLConnection) new URL(path)

.openConnection();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。关于Java实现响应重定向并发送POST请求的操作示例,可以参考以下代码: ```java import java.net.HttpURLConnection; import java.net.URL; import java.io.OutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class RedirectAndPost { public static void main(String[] args) throws Exception { String url = "http://www.example.com/redirect"; String postBody = "param1=value1&param2=value2"; HttpURLConnection connection = null; URL redirectUrl = new URL(url); while (true) { connection = (HttpURLConnection) redirectUrl.openConnection(); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", Integer.toString(postBody.getBytes().length)); connection.setUseCaches(false); connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(postBody.getBytes("UTF-8")); outputStream.flush(); outputStream.close(); int statusCode = connection.getResponseCode(); if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_SEE_OTHER) { String redirectLocation = connection.getHeaderField("Location"); redirectUrl = new URL(redirectLocation); continue; } else { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line = null; StringBuilder responseBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { responseBuilder.append(line); } reader.close(); String response = responseBuilder.toString(); System.out.println(response); break; } } } } ``` 这个示例代码实现了向一个URL发送POST请求,并在接收到重定向响应时自动跟随重定向并重新发送POST请求。注意,我们在第一次请求时将`setInstanceFollowRedirects`方法设置为`false`,以便我们能够手动处理重定向响应。当我们接收到重定向响应时,从响应头中获取重定向URL,并将其存储在`redirectUrl`变量中,然后继续重新发送POST请求,直到我们接收到最终响应为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值