Java Post重定向
在Java中,我们经常会使用HTTP协议进行网络通信。其中一个常见的需求是发送POST请求并处理重定向。本文将介绍如何使用Java发送POST请求,并处理重定向的情况。
1. 发送POST请求
首先,我们需要使用Java的网络库发送POST请求。可以使用HttpURLConnection
或者HttpClient
等库来实现。下面是基于HttpURLConnection
的示例代码:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpPostExample {
private final String url = "
private final String parameters = "param1=value1¶m2=value2";
public void sendPost() throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 添加请求方法
con.setRequestMethod("POST");
// 启用输入和输出流
con.setDoOutput(true);
con.setDoInput(true);
// 设置请求参数
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
// 发送POST请求
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(parameters);
wr.flush();
wr.close();
// 获取响应结果
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
}
}
上述代码中,我们首先创建一个URL
对象,指定要发送POST请求的URL地址。然后,我们通过HttpURLConnection
打开与该URL的连接。接下来,我们设置请求方法为POST,并启用输入和输出流。然后,我们设置请求参数,并将其写入输出流中。最后,我们通过获取响应结果来获取服务器的响应。
2. 处理重定向
当我们发送POST请求时,可能会遇到服务器返回重定向的情况。在HTTP协议中,重定向是指服务器要求客户端重新发送请求到另一个URL。
Java的网络库会自动处理重定向,并返回重定向后的响应结果。我们可以通过检查HTTP响应头的状态码来判断是否发生了重定向。常见的重定向状态码有301和302。
下面是处理重定向的示例代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpPostRedirectExample {
private final String url = "
private final String parameters = "param1=value1¶m2=value2";
public void sendPost() throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 添加请求方法
con.setRequestMethod("POST");
// 启用输入和输出流
con.setDoOutput(true);
con.setDoInput(true);
// 设置请求参数
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
// 发送POST请求
con.setInstanceFollowRedirects(false); // 禁止自动重定向
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(parameters);
wr.flush();
wr.close();
// 检查状态码
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String redirectUrl = con.getHeaderField("Location");
System.out.println("重定向URL:" + redirectUrl);
// 发送重定向请求
URL redirectObj = new URL(redirectUrl);
HttpURLConnection redirectCon = (HttpURLConnection) redirectObj.openConnection();
// 处理重定向
redirectCon.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(redirectCon.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
}
}
}