httpClient 发送post请求 携带四个参数 String domainId,String tableName,String date,String keyword
时间: 2024-06-09 11:08:15 浏览: 182
HTTP GET/POST传递参数
5星 · 资源好评率100%
可以使用以下代码来发送带参数的POST请求:
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
String url = "http://example.com/api";
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 设置请求参数
String domainId = "exampleDomainId";
String tableName = "exampleTableName";
String date = "2022-01-01";
String keyword = "exampleKeyword";
String requestParams = "{\"domainId\":\"" + domainId + "\",\"tableName\":\"" + tableName + "\",\"date\":\"" + date + "\",\"keyword\":\"" + keyword + "\"}";
httpPost.setEntity(new StringEntity(requestParams));
// 发送请求
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
// 处理响应
String responseString = EntityUtils.toString(entity);
System.out.println(responseString);
}
}
```
这个例子使用了Apache HttpComponents库,所以需要在项目中添加以下依赖:
```xml
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
```
阅读全文