java 发送一个post请求 用form-data格式 请求头的requestType-Content设置为ModelingStand
时间: 2024-05-09 10:20:35 浏览: 87
ard
可以使用Java的HttpURLConnection类来发送POST请求,并设置请求头为ModelingStandard。以下是示例代码:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostFormDataExample {
public static void main(String[] args) throws IOException {
// 设置请求URL
URL url = new URL("https://example.com/api");
// 创建连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// 设置请求头
conn.setRequestProperty("requestType-Content", "ModelingStandard");
// 设置请求正文
String requestBody = "name=John&age=30";
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
// 发送请求并获取响应
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response Body: " + response.toString());
}
}
}
```
阅读全文