try { String param1 = URLEncoder.encode("value1", StandardCharsets.UTF_8); String param2 = URLEncoder.encode("value2", StandardCharsets.UTF_8); String urlString = "https://example.com/api/resource?param1=" + param1 + "¶m2=" + param2; URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); String responseBody = response.toString(); System.out.println(responseBody); } } catch (Exception e) { e.printStackTrace(); }
java发送get请求
最新推荐文章于 2024-08-31 14:09:04 发布