package net.radar.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 工具類 - Http模擬工具類
*/
public class HttpUtil {
private static Log log = LogFactory.getLog(HttpUtil.class);
/**
* 執(zhí)行一個HTTP GET請求侯谁,返回請求響應(yīng)的HTML
*
* @param url 請求的URL地址
* @param queryString 請求的查詢參數(shù),可以為null
* @return 返回請求響應(yīng)的HTML
*/
public static String doGet(String url, String queryString) {
String response = null;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
try {
if (StringUtils.isNotBlank(queryString))
method.setQueryString(URIUtil.encodeQuery(queryString));
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
} catch (URIException e) {
log.error("執(zhí)行HTTP Get請求時,編碼查詢字符串“" + queryString + "”發(fā)生異常章钾!", e);
} catch (IOException e) {
log.error("執(zhí)行HTTP Get請求" + url + "時墙贱,發(fā)生異常!", e);
} finally {
method.releaseConnection();
}
return response;
}
/**
* 執(zhí)行一個HTTP POST請求贱傀,返回請求響應(yīng)的HTML
*
* @param url 請求的URL地址
* @param params 請求的查詢參數(shù),可以為null
* @return 返回請求響應(yīng)的HTML
*/
public static String doPost(String url, Map<String, String> params) {
String response = null;
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//method.addRequestHeader("Content-Type", "text/html; charset=UTF-8");
method.getParams().setContentCharset("UTF-8");
try {
//設(shè)置Http Post數(shù)據(jù)
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
// nameValuePairs.add(new NameValuePair(entry.getKey(),entry.getValue()));
method.addParameter(entry.getKey(), entry.getValue());
}
// method.setParams(p);
// method.addParameter(entry.getKey(), entry.getValue());
// NameValuePair [] ts = (NameValuePair [])nameValuePairs.toArray();
// method.setRequestBody(ts);
}
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
} catch (IOException e) {
log.error("執(zhí)行HTTP Post請求" + url + "時惨撇,發(fā)生異常!", e);
} finally {
method.releaseConnection();
}
return response;
}
}
Java發(fā)送HTTP的Get和Post請求
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門剔猿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來见芹,“玉大人耕陷,你說我怎么就攤上這事渗蟹∠裙荩” “怎么了栋荸?”我有些...
- 文/不壞的土叔 我叫張陵豪娜,是天一觀的道長吁津。 經(jīng)常有香客問我棚蓄,道長,這世上最難降的妖魔是什么碍脏? 我笑而不...
- 正文 為了忘掉前任梭依,我火速辦了婚禮,結(jié)果婚禮上典尾,老公的妹妹穿的比我還像新娘役拴。我一直安慰自己,他們只是感情好钾埂,可當(dāng)我...
- 文/花漫 我一把揭開白布河闰。 她就那樣靜靜地躺著科平,像睡著了一般。 火紅的嫁衣襯著肌膚如雪姜性。 梳的紋絲不亂的頭發(fā)上瞪慧,一...
- 文/蒼蘭香墨 我猛地睜開眼乌询,長吁一口氣:“原來是場噩夢啊……” “哼榜贴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起楣责,我...
- 正文 年R本政府宣布,位于F島的核電站凌蔬,受9級特大地震影響露懒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砂心,卻給世界環(huán)境...
- 文/蒙蒙 一懈词、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辩诞,春花似錦坎弯、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽撩炊。三九已至,卻和暖如春褐桌,著一層夾襖步出監(jiān)牢的瞬間衰抑,已是汗流浹背象迎。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 發(fā)送HTTP請求有很多包可以實現(xiàn),這里介紹OkHttpClient3(想了解其他一些實現(xiàn)方法和他們之間的區(qū)別劫乱,請戳...
- ** Http請求指的是客戶端向服務(wù)器的請求消息衷戈,Http請求主要分為get或post兩種狭吼,在Linux系統(tǒng)...
- http://www.cnblogs.com/smyhvae/p/4006009.html android 5.0...
- UICollectionView 不滿一屏?xí)r,無法滾動殖妇,需要如下設(shè)置 self.collectionView.al...