Linux 拨号vps windows公众号手机端

java重定向设置header的方法是什么

lewis 7年前 (2019-01-10) 阅读数 10 #程序编程
文章标签 Java

在Java中,可以使用HttpURLConnection类来设置HTTP请求的header。具体的方法是通过调用URLConnection的setRequestProperty方法来设置header。

以下是一个示例代码:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置header
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 发送请求并获取响应
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
}
}

在上面的示例中,使用setRequestProperty方法来设置User-Agent和Accept-Language两个header。可以根据需要设置其他的header。最后,调用getResponseCode方法来发送请求并获取响应的HTTP状态码。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门