gradle httpclient
时间: 2023-09-04 07:06:58 浏览: 187
如果你想在 Gradle 项目中使用 HttpClient,你可以通过在 `build.gradle` 文件中添加以下依赖来引入 HttpClient:
```groovy
dependencies {
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
}
```
然后,你可以在项目中使用 HttpClient 来发送 HTTP 请求。下面是一个使用 HttpClient 发送 GET 请求的示例:
```java
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.example.com/");
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status code: " + statusCode);
httpClient.close();
}
}
```
请注意,上面的代码需要处理异常。在实际应用中,你应该考虑使用 try-catch 块来处理可能的异常。
阅读全文