POST /api/walzzerpapi.asq HTTP/1.1 Host: www.walzz.com Content-Type: application/json cache-control: no-cache Postman-Token: 286f9b9a-e6ee-4e83-b37b-aae5d6ea7a5e { "Appkey": "mEwead5ZQx0psM6b", "Sign": "9d5cd39aab1adb6837e044e2eed0dd82", "Timestamp": 1690161912433, "TargetNodeId": "gdwalzz", "TargetDatabaseId": "WalzzPaperERP", "QueryID": 0, "SQLCommand": "select * from Users" }------WebKitFormBoundary7MA4YWxkTrZu0gW--,这是postman给出来的,给我提供一个封装好的java方法,做http请求。参考上面的
时间: 2023-08-12 16:06:21 浏览: 626
以下是一个封装好的 Java 方法,用于进行 HTTP 请求:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequestUtil {
public static String sendPostRequest(String url, String jsonInput) throws IOException {
URL requestUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("cache-control", "no-cache");
connection.setRequestProperty("Postman-Token", "286f9b9a-e6ee-4e83-b37b-aae5d6ea7a5e");
connection.setDoOutput(true);
try (OutputStream outputStream = connection.getOutputStream()) {
byte[] input = jsonInput.getBytes("utf-8");
outputStream.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
return response.toString();
} else {
throw new IOException("HTTP request failed with code: " + responseCode);
}
}
public static void main(String[] args) {
try {
String url = "http://www.walzz.com/api/walzzerpapi.asq";
String jsonInput = "{\"Appkey\":\"mEwead5ZQx0psM6b\",\"Sign\":\"9d5cd39aab1adb6837e044e2eed0dd82\",\"Timestamp\":1690161912433,\"TargetNodeId\":\"gdwalzz\",\"TargetDatabaseId\":\"WalzzPaperERP\",\"QueryID\":0,\"SQLCommand\":\"select * from Users\"}";
String response = sendPostRequest(url, jsonInput);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请确保你已经将 `url` 和 `jsonInput` 替换为你实际的请求 URL 和 JSON 输入。这个方法会发送 POST 请求,并返回服务器的响应。
阅读全文