Java發(fā)送post方法詳解
總結(jié)一下java使用http發(fā)送post的方法:
1、post請(qǐng)求用于發(fā)送json 格式的參數(shù):
/**
* post請(qǐng)求(用于請(qǐng)求json格式的參數(shù))
*
* @param url 地址
* @param params json格式的參數(shù)
* @return
*/
public static String doPost(String url, String params) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost( url );// 創(chuàng)建httpPost
httpPost.setHeader( "Accept", "application/json" );
httpPost.setHeader( "Content-Type", "application/json" );
String charSet = "UTF-8";
StringEntity entity = new StringEntity( params, charSet );
httpPost.setEntity( entity );
CloseableHttpResponse response = null;
try {
response = httpclient.execute( httpPost );
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString( responseEntity );
return jsonString;
} else {
logger.error( "請(qǐng)求返回:" + state + "(" + url + ")" );
}
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
2、用于發(fā)送key-value格式的參數(shù)
/**
* post請(qǐng)求(用于key-value格式的參數(shù))
*
* @param url
* @param params
* @return
*/
public static String doPost(String url, Map params) {
BufferedReader in = null;
try {
// 定義HttpClient
HttpClient client = new DefaultHttpClient();
// 實(shí)例化HTTP方法
HttpPost request = new HttpPost();
request.setURI( new URI( url ) );
//設(shè)置參數(shù)
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) {
String name = (String) iter.next();
String value = String.valueOf( params.get( name ) );
nvps.add( new BasicNameValuePair( name, value ) );
//System.out.println(name +"-"+value);
}
request.setEntity( new UrlEncodedFormEntity( nvps, HTTP.UTF_8 ) );
HttpResponse response = client.execute( request );
int code = response.getStatusLine().getStatusCode();
if (code == 200) { //請(qǐng)求成功
in = new BufferedReader( new InputStreamReader( response.getEntity()
.getContent(), "utf-8" ) );
StringBuffer sb = new StringBuffer( "" );
String line = "";
String NL = System.getProperty( "line.separator" );
while ((line = in.readLine()) != null) {
sb.append( line + NL );
}
in.close();
return sb.toString();
} else { //
System.out.println( "狀態(tài)碼:" + code );
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
第三,發(fā)送get請(qǐng)求
/**
* get請(qǐng)求
*
* @return
*/
public static String doGet(String url) {
try {
HttpClient client = new DefaultHttpClient();
//發(fā)送get請(qǐng)求
HttpGet request = new HttpGet( url );
HttpResponse response = client.execute( request );
/**請(qǐng)求發(fā)送成功,并得到響應(yīng)**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**讀取服務(wù)器返回過(guò)來(lái)的json字符串?dāng)?shù)據(jù)**/
String strResult = EntityUtils.toString( response.getEntity() );
return strResult;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
以上所述是小編給大家介紹的Java發(fā)送post方法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 解決java使用axios.js的post請(qǐng)求后臺(tái)時(shí)無(wú)法接收到入?yún)⒌膯?wèn)題
- java 通過(guò)發(fā)送json,post請(qǐng)求,返回json數(shù)據(jù)的方法
- java模擬post請(qǐng)求發(fā)送json的例子
- 淺談Java代碼的 微信長(zhǎng)鏈轉(zhuǎn)短鏈接口使用 post 請(qǐng)求封裝Json(實(shí)例)
- JAVA發(fā)送http get/post請(qǐng)求,調(diào)用http接口、方法詳解
- Java使用JDBC連接postgresql數(shù)據(jù)庫(kù)示例
- Java后臺(tái)基于POST獲取JSON格式數(shù)據(jù)
相關(guān)文章
Java中Object類常用的12個(gè)方法(小結(jié))
Java 中的 Object 方法在面試中是一個(gè)非常高頻的點(diǎn),本文主要介紹了Java中Object類常用的12個(gè)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法
這篇文章主要介紹了Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無(wú)限制整數(shù)的逆序輸出,第二種是非負(fù)整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細(xì)需要的朋友可以參考下2022-11-11
Java構(gòu)造方法實(shí)例詳解(動(dòng)力節(jié)點(diǎn)java學(xué)院整理)
其實(shí)java構(gòu)造方法很簡(jiǎn)單,下面通過(guò)示例給大家分享java構(gòu)造方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-04-04
java?Date獲取本月的開(kāi)始時(shí)間與結(jié)束時(shí)間
這篇文章主要為大家介紹了java?Date獲取本月的開(kāi)始時(shí)間與結(jié)束時(shí)間示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05
解析WeakHashMap與HashMap的區(qū)別詳解
本篇文章是對(duì)WeakHashMap與HashMap的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Spring?Boot中調(diào)用外部接口的3種方式步驟
這篇文章主要給大家介紹了關(guān)于Spring?Boot中調(diào)用外部接口的3種方式步驟,在Spring-Boot項(xiàng)目開(kāi)發(fā)中,存在著本模塊的代碼需要訪問(wèn)外面模塊接口,或外部url鏈接的需求,需要的朋友可以參考下2023-08-08
Java HashMap在遍歷時(shí)刪除元素的實(shí)現(xiàn)
本文主要介紹了Java HashMap在遍歷時(shí)刪除元素的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12

