autojs http post
时间: 2023-07-11 13:44:02 浏览: 106
在Auto.js中发送HTTP POST请求可以使用Java的HttpURLConnection类或者OkHttp库。以下是一个使用HttpURLConnection类发送POST请求的示例代码:
```
var url = "http://example.com/api";
var data = "param1=value1¶m2=value2";
var conn = new java.net.URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
var outputStream = conn.getOutputStream();
outputStream.write(data.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
var responseCode = conn.getResponseCode();
var response = "";
if (responseCode == java.net.HttpURLConnection.HTTP_OK) {
var inputStream = conn.getInputStream();
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
var line;
while ((line = reader.readLine()) != null) {
response += line;
}
reader.close();
inputStream.close();
} else {
response = "HTTP error: " + responseCode;
}
```
这段代码将在5秒内向`http://example.com/api`发送一个POST请求,请求参数为`param1=value1¶m2=value2`,并读取响应内容。你可以根据实际需要修改URL、请求参数和超时时间等参数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)