java如何向python发送http请求
时间: 2023-10-11 16:10:03 浏览: 98
java发送http请求
5星 · 资源好评率100%
Java可以使用Apache HttpComponents或者Java原生的URLConnection类来向Python发送HTTP请求。下面是使用HttpComponents发送GET请求的示例代码:
```
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://example.com/api");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
try {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
String responseString = EntityUtils.toString(entity);
System.out.println(responseString);
}
} finally {
httpResponse.close();
}
```
这个示例代码中,我们使用了HttpComponents的HttpClient类来创建一个HTTP客户端,然后使用HttpGet类创建一个GET请求,并发送到指定的URL。最后,我们读取响应的实体并将其转换为字符串输出。
阅读全文