您的位置:

Java发送Post请求FormData详解

一、SSH发送Post请求

1、SSH简介

SSH即为安全壳(Secure Shell),用于在计算机之间的加密连接,主要用于远程登录和执行命令。以下以SSH发送Post请求的实现为例。

2、SSH发送Post请求FormData代码示例

String url = "http://localhost:8080/post";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = {
        new NameValuePair("name", "java"),
        new NameValuePair("id", "123456")
};
postMethod.addParameters(data);
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();

二、JAVA发送Post请求

1、JAVA发送Post请求简介

JAVA发送Post请求,可以通过HttpURLConnection实现。以下以JAVA发送Post请求FormData的实现为例。

2、JAVA发送Post请求FormData代码示例

String url = "http://localhost:8080/post";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "name=java&id=123456";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
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();

三、PHP发送Post请求

1、PHP发送Post请求简介

PHP发送Post请求可以通过curl函数实现。以下以PHP发送Post请求FormData的实现为例。

2、PHP发送Post请求FormData代码示例

$url = "http://localhost:8080/post";
$data = array('name' => 'java', 'id' => '123456');
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

四、JSP发送Post请求

1、JSP发送Post请求简介

JSP发送Post请求可以通过HttpClient实现。以下以JSP发送Post请求FormData的实现为例。

2、JSP发送Post请求FormData代码示例

String url = "http://localhost:8080/post";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = {
        new NameValuePair("name", "java"),
        new NameValuePair("id", "123456")
};
postMethod.addParameters(data);
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();

五、结尾

本文从不同编程语言分别讲解了如何发送Post请求FormData,相信读者在实际开发中有所收益。希望读者在使用Post请求时,能够注意数据安全问题,维护用户隐私。感谢阅读!