新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

java重定向设置header的有哪些方法,javaee重定向

发布时间:2023-08-08 07:56:20

java重定向设置header的有哪些方法

在Java中,可使用HttpURLConnection类来设置HTTP要求的header。具体的方法是通过调用URLConnection的setRequestProperty方法来设置header。
以下是一个示例代码:
```java
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状态码。