資源簡介
java發(fā)送post和get請求源碼及jar包

代碼片段和文件信息
package?com.pjj.myutil.util;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.UnsupportedEncodingException;
import?java.net.MalformedURLException;
import?java.net.URL;
import?java.net.URLConnection;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Map;
import?org.apache.http.HttpEntity;
import?org.apache.http.HttpResponse;
import?org.apache.http.NameValuePair;
import?org.apache.http.ParseException;
import?org.apache.http.client.ClientProtocolException;
import?org.apache.http.client.entity.UrlEncodedFormEntity;
import?org.apache.http.client.methods.HttpPost;
import?org.apache.http.impl.client.DefaultHttpClient;
import?org.apache.http.message.BasicNameValuePair;
import?org.apache.http.protocol.HTTP;
import?org.apache.http.util.EntityUtils;
public?class?PostAndGetTest?{
public?final?static?String?postUrl=“http://localhost:8080/VMPost/SendPost“;
public?final?static?String?getUrl=“http://172.17.254.228:8088/monitor/monitorData/listMonitorDatas“;
/**
?*?發(fā)送post請求
?*?url?請求路徑
?*?參數(shù)在方法內(nèi)封裝,改寫成工具類的話,直接傳參也可
?*/
public?String?sendPost(String?url){
String?result=““;
//設(shè)置參數(shù)
List?params?=?new?ArrayList();
params.add(new?BasicNameValuePair(“resourceUniqueKey“?“1“));
params.add(new?BasicNameValuePair(“key“?“1“));
params.add(new?BasicNameValuePair(“scope“?“60“));
params.add(new?BasicNameValuePair(“startTime“?“Fri?Oct?21?2016?12:38:12?GMT+0800?(中國標(biāo)準(zhǔn)時(shí)間)“));
params.add(new?BasicNameValuePair(“endTime“?“Fri?Oct?21?2016?15:38:12?GMT+0800?(中國標(biāo)準(zhǔn)時(shí)間)“));
//?創(chuàng)建HttpPost對象
HttpPost?httpRequst?=?new?HttpPost(url);
try?{
//?post請求參數(shù)寫入httprequest對象
httpRequst.setEntity(new?UrlEncodedFormEntity(params?HTTP.UTF_8));
//?發(fā)送請求
HttpResponse?httpResponse?=?new?DefaultHttpClient().execute(httpRequst);
//?http響應(yīng)代碼200為成功
Integer?statusCode=httpResponse.getStatusLine().getStatusCode();
if?(statusCode?==?200)?{
//?取出應(yīng)答字符串
HttpEntity?httpEntity?=?httpResponse.getEntity();
result?=?EntityUtils.toString(httpEntity);
}else{
return?statusCode.toString();
}
}?catch?(UnsupportedEncodingException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(ClientProtocolException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(ParseException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
return?result;
}
/**
?*?發(fā)送get請求
?*?url?請求路徑
?*?參數(shù)在方法內(nèi)封裝,改寫成工具類的話,直接傳參也可
?*/
public?String?sendGet(String?url){
String?result?=?““;
????????BufferedReader?in?=?null;
????????//get請求使用url傳參
????????String?params=“?resourceUniqueKey=1&formula=avg&key=system.cpu.util[user]&scope=60&startTime=1476936000&endTime=1476946800“;
try?{
URL?realUrl?=?new?URL(url+params);
/
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????60841??2016-10-20?13:16??http\commons-logging-1.1.1.jar
?????文件?????428353??2016-10-20?13:12??http\httpclient-4.2.2.jar
?????文件?????223571??2016-10-20?13:12??http\httpcore-4.2.2.jar
?????文件???????4693??2016-10-21?17:08??http\PostAndGetTest.java
?????目錄??????????0??2016-10-22?10:34??http
-----------?---------??----------?-----??----
???????????????717458????????????????????5
評論
共有 條評論