在 Java 中发送 POST 请求并传递多个参数可以使用 HttpURLConnection 类。以下是一个简单的代码示例:
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
public class HttpPost {
public static void sendPost(String url, Map<String, String> parameters) throws Exception {
HttpURLConnection httpConn = (HttpURLConnection) new URL(url).openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
OutputStream os = httpConn.getOutputStream();
os.write(getPostData(parameters).getBytes());
os.flush();
os.close();
int responseCode = httpConn.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("POST Request is successful");
} else {
System.out.println("POST Request is unsuccessful");
private static String getPostData(Map<String, String> parameters) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : parameters.entrySet()) {
if (first) {
first = false;
} else {
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
return result.toString();
Gerry0808