java调用http接口get请求提交参数
时间: 2023-04-05 15:03:19 浏览: 99
通过Java发送http请求
可以使用Java中的HttpURLConnection或者HttpClient来实现。具体实现可以参考以下代码:
使用HttpURLConnection:
```
URL url = new URL("http://example.com/api?param1=value1¶m2=value2");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
```
使用HttpClient:
```
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://example.com/api?param1=value1¶m2=value2");
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(result);
response.close();
httpClient.close();
```
阅读全文