Java 使用httpClient 发送post请求 携带四个参数 String domainId,String tableName,String date,String keyword
时间: 2024-06-09 08:08:14 浏览: 158
java使用httpclient发送post请求示例
3星 · 编辑精心推荐
可以使用以下代码发送post请求并携带四个参数:
```java
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://example.com");
// 设置参数
String domainId = "your_domain_id";
String tableName = "your_table_name";
String date = "your_date";
String keyword = "your_keyword";
String json = String.format("{\"domainId\":\"%s\",\"tableName\":\"%s\",\"date\":\"%s\",\"keyword\":\"%s\"}",
domainId, tableName, date, keyword);
httpPost.setEntity(new StringEntity(json));
// 发送请求
httpClient.execute(httpPost);
}
}
```
其中,`http://example.com` 是你要发送请求的地址,`domainId`、`tableName`、`date`、`keyword` 是要携带的四个参数。将这些参数放到一个 JSON 对象中,然后将这个对象作为请求体发送。
阅读全文