java網(wǎng)絡(luò)編程中向指定URL發(fā)送GET POST請求示例
更新時間:2013年11月25日 15:38:04 作者:
這篇文章主要介紹了java向指定URL發(fā)送GET POST請求示例,學(xué)習(xí)JAVA網(wǎng)絡(luò)編程一定會用到的,大家參考使用吧
復(fù)制代碼 代碼如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
/**
* 向指定URL發(fā)送GET方法的請求
*
* @param url
* 發(fā)送請求的URL
* @param param
* 請求參數(shù),請求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。
* @return URL 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設(shè)置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實際的連接
connection.connect();
// 獲取所有響應(yīng)頭字段
/*
* Map<String, List<String>> map = connection.getHeaderFields(); //
* 遍歷所有的響應(yīng)頭字段 for (String key : map.keySet()) {
* System.out.println(key + "--->" + map.get(key)); }
*/
// 定義 BufferedReader輸入流來讀取URL的響應(yīng)
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送GET請求出現(xiàn)異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關(guān)閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 發(fā)送POST方法的請求
*
* @param url
* 發(fā)送請求的 URL
* @param param
* 請求參數(shù),請求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。
* @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設(shè)置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發(fā)送POST請求必須設(shè)置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應(yīng)的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發(fā)送請求參數(shù)
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應(yīng)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送 POST 請求出現(xiàn)異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關(guān)閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
您可能感興趣的文章:
- JAVA發(fā)送http get/post請求,調(diào)用http接口、方法詳解
- java模擬http的Get/Post請求,并設(shè)置ip與port代理的方法
- java發(fā)送http的get、post請求實現(xiàn)代碼
- java實現(xiàn)http的Post、Get、代理訪問請求
- Java模擬HTTP Get Post請求 輕松實現(xiàn)校園BBS自動回帖
- Java如何實現(xiàn)URL帶請求參數(shù)(get/post)及得到get和post請求url和參數(shù)列表的方法
- java發(fā)送get請求和post請求示例
- java使用httpclient模擬post請求和get請求示例
- Java commons-httpclient如果實現(xiàn)get及post請求
相關(guān)文章
mybatis-plus之如何實現(xiàn)in嵌套sql
這篇文章主要介紹了mybatis-plus之如何實現(xiàn)in嵌套sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot采用Dynamic-Datasource方式實現(xiàn)多JDBC數(shù)據(jù)源
在某些情況下,如果我們需要配置多個數(shù)據(jù)源,本文主要介紹了SpringBoot采用Dynamic-Datasource方式實現(xiàn)多JDBC數(shù)據(jù)源,具有一定的參考價值,感興趣的可以了解一下2023-10-10從Android源碼剖析Intent查詢匹配的實現(xiàn)
這篇文章主要介紹了從Android源碼剖析Intent查詢匹配的實現(xiàn),Intent部分的源碼為Java代碼,需要的朋友可以參考下2015-07-07SpringCloud Netflix Ribbon源碼解析(推薦)
這篇文章主要介紹了SpringCloud Netflix Ribbon源碼解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03詳解Mybatis攔截器安全加解密MySQL數(shù)據(jù)實戰(zhàn)
本文主要介紹了Mybatis攔截器安全加解密MySQL數(shù)據(jù)實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01mybatis-plus QueryWrapper and or 連用并且實現(xiàn)分
這篇文章主要介紹了mybatis-plus QueryWrapper and or 連用并且實現(xiàn)分頁,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01spring?boot+vue實現(xiàn)JSAPI微信支付的完整步驟
JSAPI支付是用戶在微信中打開商戶的H5頁面,商戶在H5頁面通過調(diào)用微信支付提供的JSAPI接口調(diào)起微信支付模塊完成支付,下面這篇文章主要給大家介紹了關(guān)于spring?boot+vue實現(xiàn)JSAPI微信支付的相關(guān)資料,需要的朋友可以參考下2022-05-05