用Java写一个Apache http请求案例
时间: 2023-03-29 13:04:24 浏览: 129
可以的,以下是一个简单的Java代码示例,用于发送Apache http请求:
```java
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ApacheHttpRequestExample {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.example.com");
String response = httpClient.execute(httpGet, response -> EntityUtils.toString(response.getEntity()));
System.out.println(response);
}
}
```
这个示例使用了Apache HttpClient库,发送了一个GET请求到`https://www.example.com`,并输出了响应内容。
阅读全文