String jsonInput = "{"Appkey":"mEwead5ZQx0psM6b","Sign":"9d5cd39aab1adb6837e044e2eed0dd82","Timestamp":Timestamp,"TargetNodeId":"gdwalzz","TargetDatabaseId":"WalzzPaperERP","QueryID":0,"SQLCommand":"select * from Users"}";,mEwead5ZQx0psM6b,9d5cd39aab1adb6837e044e2eed0dd82等等几个,替换成变量提供给我,变量就用Appkey,Sign以此类推
时间: 2023-08-05 16:06:46 浏览: 57
以下是使用变量替换的代码示例:
```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 appKey, String sign, long timestamp, String targetNodeId, String targetDatabaseId, int queryId, String sqlCommand) 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);
String jsonInput = String.format("{\"Appkey\":\"%s\",\"Sign\":\"%s\",\"Timestamp\":%d,\"TargetNodeId\":\"%s\",\"TargetDatabaseId\":\"%s\",\"QueryID\":%d,\"SQLCommand\":\"%s\"}",
appKey, sign, timestamp, targetNodeId, targetDatabaseId, queryId, sqlCommand);
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 appKey = "mEwead5ZQx0psM6b";
String sign = "9d5cd39aab1adb6837e044e2eed0dd82";
long timestamp = 1690161912433L;
String targetNodeId = "gdwalzz";
String targetDatabaseId = "WalzzPaperERP";
int queryId = 0;
String sqlCommand = "select * from Users";
String response = sendPostRequest(url, appKey, sign, timestamp, targetNodeId, targetDatabaseId, queryId, sqlCommand);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我添加了新的参数并将它们传递给 `sendPostRequest` 方法。你可以使用这些参数来动态构建 JSON 输入字符串。确保将这些变量替换为你实际的值。