一、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請求時,能夠注意數據安全問題,維護用戶隱私。感謝閱讀!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/196281.html
微信掃一掃
支付寶掃一掃