java httpurlconnection
时间: 2023-04-28 14:04:59 浏览: 45
Java中的HttpURLConnection是一个用于发送和接收HTTP请求和响应的类。它是Java标准库中的一部分,可以通过URL.openConnection()方法创建一个连接。HttpURLConnection提供了许多方法来设置请求头、请求体、请求方法、响应码、响应头等信息,并可以通过输入流和输出流来读取和写入数据。它是Java中处理HTTP请求和响应的重要工具之一。
相关问题
java httpurlconnection 上传
Java的HttpURLConnection类可以用于实现文件的上传。具体步骤如下:
1. 创建URL对象,指定上传的目标地址。
2. 打开URL对象的连接,并转换为HttpURLConnection对象。
3. 设置请求方法为POST,并开启输出流,以便写入数据。
4. 设置请求头部信息,包含文件的内容类型、长度等。
5. 创建文件输入流,读取待上传的文件。
6. 创建缓冲区,并通过字节流将文件数据写入缓冲区。
7. 将缓冲区的数据写入输出流,通过URL对象发送到目标地址。
8. 获取服务器返回的响应码,判断上传是否成功。
9. 关闭输入流、输出流和连接。
以下为示例代码:
```
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUploader {
public static void main(String[] args) {
String targetUrl = "http://example.com/upload";
String filePath = "path/to/file.txt";
try {
URL url = new URL(targetUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
File file = new File(filePath);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + "*****");
connection.setRequestProperty("Content-Length", String.valueOf(file.length()));
FileInputStream fileInputStream = new FileInputStream(file);
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
dataOutputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
int responseCode = connection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = "";
String inputLine;
while ((inputLine = in.readLine()) != null) {
response += inputLine;
}
in.close();
System.out.println("上传结果:" + response);
System.out.println("响应码:" + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上是使用Java的HttpURLConnection类实现文件上传的基本步骤和示例代码。根据需要,你可以根据实际情况进行修改和调整。
Java HttpURLConnection遵循重定向示例
以下是使用Java HttpURLConnection遵循重定向的示例代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
public static void main(String[] args) throws IOException {
String url = "https://www.example.com";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setInstanceFollowRedirects(false); // 禁用自动重定向
// 发送GET请求
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
|| responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
// 获取重定向URL
String redirectUrl = conn.getHeaderField("Location");
// 手动发送重定向请求
URL newUrl = new URL(redirectUrl);
conn = (HttpURLConnection) newUrl.openConnection();
conn.setRequestMethod("GET");
responseCode = conn.getResponseCode();
}
// 处理响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
```
在该示例中,我们首先发送一个GET请求,并检查响应代码是否为重定向代码(HTTP_MOVED_TEMP或HTTP_MOVED_PERM)。如果是,我们手动获取重定向URL并发送另一个GET请求。最后,我们处理响应并将其输出到控制台。注意,我们在第一个连接对象上禁用了自动重定向,以便我们可以手动处理重定向。