叠甲:大佬轻喷,仅供自己参考,有合适的可以参考。不合适的,在这里道歉,没有帮上您
package org.example;
public class init {
public static void main(String[] args) {
String urlString = "http://example.com/form-endpoint";
String param1 = "key1";
String value1 = "value1";
String param2 = "key2";
String value2 = "value2";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream os = connection.getOutputStream()) {
String query = String.format(
"%s=%s&%s=%s",
URLEncoder.encode(param1, "UTF-8"),
URLEncoder.encode(value1, "UTF-8"),
URLEncoder.encode(param2, "UTF-8"),
URLEncoder.encode(value2, "UTF-8"));
}
int responseCode = connection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response Code : " + responseCode);
System.out.println("Response : " + response.toString());
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}