兩種訪問介面的方式(get和post)
跨機器、跨語言的遠端訪問形式一共有三種:scoket傳送資料包、http傳送請求、rmi遠端連線;
http傳送請求方式;分為post和get兩種方式
importjava.io.IOException;
importjava.io.InputStream;
import java.util.Map;
importjava.util.concurrent.atomic.AtomicInteger;
importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.HttpException;
importorg.apache.commons.httpclient.HttpStatus;
importorg.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
importorg.apache.commons.httpclient.NameValuePair;
importorg.apache.commons.httpclient.methods.GetMethod;
importorg.apache.commons.httpclient.methods.PostMethod;
importorg.apache.commons.httpclient.params.HttpConnectionManagerParams;
importorg.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
publicclass SendHttpUrl {
privatefinalstatic Logger logger = LoggerFactory.getLogger(SendHttpUrl.class);
privatestatic HttpClient httpClient = null;
privatestaticMultiThreadedHttpConnectionManager connectionManager = null;// 多執行緒管理器
privateintmaxThreadsTotal = 128;// 最大執行緒數
privateintmaxThreadsPerHost = 32; // 分配給每個客戶端的最大執行緒數
privateintconnectionTimeout = 15000;// 連線超時時間,毫秒
privateintsoTimeout = 14000;// 讀取資料超時時間,毫秒
publicvoid init() {
connectionManager = newMultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = newHttpConnectionManagerParams();
params.setConnectionTimeout(connectionTimeout);
params.setMaxTotalConnections(maxThreadsTotal);
params.setSoTimeout(soTimeout);
if (maxThreadsTotal > maxThreadsPerHost) {
params.setDefaultMaxConnectionsPerHost(maxThreadsPerHost);
} else {
params.setDefaultMaxConnectionsPerHost(maxThreadsTotal);
}
connectionManager.setParams(params);
httpClient = new HttpClient(connectionManager);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
}
/**
* get方式訪問
*
* @param url
* @param callType
* @param parmMap
* @return
*/
private String callByGet(Stringurl, String callType, Map<String, String> parmMap) {
if (logger.isDebugEnabled()){
logger.debug("in get,url:" + url);
}
GetMethod getMethod = new GetMethod(url);
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(getMethod);
if (logger.isDebugEnabled()){
logger.debug("inget,statusCode:" + statusCode);
}
if (statusCode != HttpStatus.SC_OK) {
// 訪問失敗
logger.error("in get,訪問appagent失敗。網路問題。statusCode:" + statusCode);
returnnull;
}
InputStream inputStream = getMethod.getResponseBodyAsStream();
if (inputStream == null) {
// 訪問正常:獲取到的資料為空
logger.error("in get,從appagent返回的資料為空!");
returnnull;
}
String rtn = IOUtils.toString(inputStream, "utf-8");
return rtn;
} catch (HttpException e) {
logger.error("get方式訪問appagent失敗!", e);
returnnull;
} catch (IOException e) {
logger.error("get方式訪問appagent失敗!", e);
returnnull;
} finally {
getMethod.releaseConnection();
}
}
/**
* post方式訪問
*
* @param url
* @param callType
* @param parmMap
* @return
*/
private String callByPost(Stringurl, String callType, Map<String, String> parmMap) {
if (logger.isDebugEnabled()){
logger.debug("inpost,url:" + url);
}
PostMethod p = new PostMethod(url);
if (parmMap != null) {
NameValuePair[] params = newNameValuePair[parmMap.size()];
AtomicInteger atomicInteger = new AtomicInteger(0);
for (Map.Entry<String, String> parm :parmMap.entrySet()) {
NameValuePair parmValue = newNameValuePair(parm.getKey(), parm.getValue());
params[atomicInteger.getAndIncrement()] = parmValue;
}
p.setRequestBody(params);
}
try {
int statusCode = httpClient.executeMethod(p);
logger.debug("inget,statusCode:" + statusCode);
if (statusCode != HttpStatus.SC_OK) {
// 異常
logger.error("in post,訪問appagent失敗。網路問題。statusCode:" + statusCode);
returnnull;
}
InputStream inputStream = p.getResponseBodyAsStream();
if (inputStream == null) {
// 訪問正常
logger.error("in post,從appagent返回的資料為空!");
returnnull;
}
String rtn = IOUtils.toString(inputStream, "utf-8");
return rtn;
} catch (HttpException e) {
logger.error("post方式訪問appagent失敗!", e);
returnnull;
} catch (IOException e) {
logger.error("post方式訪問appagent失敗!", e);
returnnull;
} finally {
p.releaseConnection();
}
}
}
相關文章
- ruby 訪問新浪微博API post方式和get方式API
- GET和POST兩種基本請求方法的區別
- Vue裡邊介面訪問Post、GetVue
- JVM中物件訪問定位兩種方式JVM物件
- J2ME中兩種Http連線方式Post&Get的比較HTTP
- 客戶端 post ,get 訪問伺服器客戶端伺服器
- JAVA -get-post-soap方式反問外部webservicesJavaWeb
- GET和POST方式請求API介面資料返回API
- Python訪問Oracle的兩種資料獲取方式PythonOracle
- 引數傳遞中編碼問題(Get/Post 方式)(一)
- 引數傳遞中編碼問題(Get/Post 方式)(二)
- get和post區別
- GET 和 POST 的區別(重要,面試常問)面試
- javascript get和set訪問器JavaScript
- post方式get方式提交表單的主要區別
- ajax 請求的時候 get 和 post 方式的區別?
- 淺談HTTP中GET和POST請求方式的區別HTTP
- 解決ajax get post方式提交中文引數亂碼問題
- Spring框架訪問資料庫的兩種方式的小案例Spring框架資料庫
- gitlab兩種連線方式:ssh和http配置介紹GitlabHTTP
- request的get和post引數亂碼問題
- GET和POST的區別?
- GET和POST的區別
- GET 和 POST 的區別
- POST 和 GET 的區別
- jQuery – AJAX get() 和 post() 方法jQuery
- Get/Post
- oracle 索引訪問的幾種方式Oracle索引
- CodeIgniter3 獲取GET/POST/PUT/DELETE方法多種方式傳參delete
- Http請求get與post請求方式的各種相關面試總結HTTP面試
- Python探析get和post方法Python
- 面試之 get 和 post 區別面試
- Get和Post請求詳解
- Get和Post、冪等、淨荷
- 三種 Post 提交資料方式
- jquery GET POSTjQuery
- About post and get
- java傳送GET和post請求Java