idea建httpclient
时间: 2023-11-21 07:57:01 浏览: 77
以下是在IntelliJ IDEA中使用httpclient的步骤:
1.打开IntelliJ IDEA,创建一个新的Java项目。
2.在项目中创建一个新的Java类。
3.在Java类中导入httpclient的依赖包,例如:
```xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
```
4.在Java类中编写httpclient的代码,例如:
```java
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");
HttpResponse response = client.execute(request);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufReader.readLine()) != null) {
builder.append(line);
builder.append(System.lineSeparator());
}
System.out.println(builder);
}
}
```
5.运行Java类,即可看到httpclient发送请求并接收响应的结果。
阅读全文