您的位置:

Java发送POST请求详解

一、Java发送POST请求

需要发送Post请求时,可以使用Java提供的HttpURLConnection类来实现。以下是发送Post请求的代码示例:

import java.io.*;
import java.net.*;

public class HttpPost {
    private static final String POST_URL = "http://example.com/path";
    private static final String POST_PARAMS = "param1=value1&param2=value2&param3=value3";

    public static void main(String[] args) throws Exception {
        URL url = new URL(POST_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("POST");
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();

        int responseCode = con.getResponseCode();
        System.out.println("Response Code : " + responseCode);

        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());
    }
}

这个例子中,我们定义了POST_URL和POST_PARAMS两个常量。POST_URL是我们要发送Post请求的目标地址,POST_PARAMS是我们要发送的参数,多个参数之间使用&连接。接下来我们使用HttpURLConnection类来打开连接,然后设置请求的方法为POST,设置是否向服务器传递数据并设置输出流。我们将POST_PARAMS写入输出流中,并关闭,然后获取服务器的响应码和响应体。

二、JSP发送POST请求

JSP程序可以使用Java的URLConnection类来发送POST请求。以下是JSP程序发送Post请求的代码示例:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.net.*,java.io.*" %>

<%
    String POST_URL = "http://example.com/path";
    String POST_PARAMS = "param1=value1&param2=value2&param3=value3";

    URL url = new URL(POST_URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    con.setRequestMethod("POST");
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();

    int responseCode = con.getResponseCode();
    out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(
        con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    out.println(response.toString());
%>

这个例子中,我们使用Java的URLConnection类来发送POST请求,几乎和Java发送Post请求时的代码一样,只是在使用out向页面输出响应码和响应体。

三、SSH发送POST请求

SSH使用HttpComponents库来发送HTTP请求。以下是SSH发送POST请求的代码示例:

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class HttpPost {
    private static final String POST_URL = "http://example.com/path";
    private static final String POST_PARAMS = "param1=value1&param2=value2&param3=value3";

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpHost proxy = new HttpHost("proxy_host", proxy_port);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        HttpPost httpPost = new HttpPost(POST_URL);

        List nvps = new ArrayList
   ();
        nvps.add(new BasicNameValuePair("param1", "value1"));
        nvps.add(new BasicNameValuePair("param2", "value2"));
        nvps.add(new BasicNameValuePair("param3", "value3"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    }
}

   
  

这个例子中,我们使用了HttpComponents库来发送POST请求。我们使用了HttpClients.createDefault()来生成一个http客户端,然后设置代理、创建HttpPost类并设置目标地址、添加参数。最后执行请求并打印响应状态和内容。

四、PHP发送POST请求

PHP使用cURL库来发送HTTP请求。以下是PHP程序发送POST请求的代码示例:

 'value1',
    'param2' => 'value2',
    'param3' => 'value3'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

$output = curl_exec($ch);

curl_close($ch);

echo $output;
?>

这个例子中,我们使用了cURL库来发送POST请求。我们首先定义了目标地址和要发送的参数。接着,我们初始化cURL,使用curl_setopt()函数来设置选项,包括目标地址、请求头、请求方式和要发送的数据等。最后关闭cURL,并输出响应内容。