更新http接口测试,httpclient
This commit is contained in:
parent
2c88033899
commit
965b61a343
9
pom.xml
9
pom.xml
|
@ -167,10 +167,15 @@
|
|||
<artifactId>commons-el</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>commons-httpclient</groupId>
|
||||
<artifactId>commons-httpclient</artifactId>
|
||||
<version>3.1</version>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
|
@ -255,7 +260,7 @@
|
|||
<dependency>
|
||||
<groupId>net.sf.json-lib</groupId>
|
||||
<artifactId>json-lib</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>2.3</version>
|
||||
<classifier>jdk15</classifier>
|
||||
</dependency>
|
||||
<!--由于Oracle JDBC驱动包是需要Oracle官方授权才能被下载,如果原maven私库中无此包,请手动添加至私库再在maven中操作update project-->
|
||||
|
|
|
@ -73,7 +73,7 @@ public class GetServerAPI {
|
|||
}
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||
|
||||
getCasesbyplanid(1);
|
||||
}
|
||||
|
||||
}
|
|
@ -13,19 +13,26 @@ import java.net.URL;
|
|||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
||||
|
||||
import luckyclient.planapi.entity.ProjectProtocolTemplate;
|
||||
import luckyclient.planapi.entity.ProjectTemplateParams;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import net.sf.json.JsonConfig;
|
||||
|
||||
/**
|
||||
* @Description:发送http请求帮助类
|
||||
|
@ -34,6 +41,7 @@ public class HttpClientHelper {
|
|||
/**
|
||||
* @Description:使用HttpURLConnection发送post请求
|
||||
*/
|
||||
static String author = "Basic " + Base64.encode(("admin" + ":" + "123456").getBytes());
|
||||
public static String sendHttpURLPost(String urlParam, Map<String, Object> params, String charset, int timeout) {
|
||||
StringBuffer resultBuffer = null;
|
||||
// 构建请求参数
|
||||
|
@ -57,7 +65,86 @@ public class HttpClientHelper {
|
|||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
if (sbParams != null && sbParams.length() > 0) {
|
||||
osw = new OutputStreamWriter(con.getOutputStream(), charset);
|
||||
osw.write(sbParams.substring(0, sbParams.length() - 1));
|
||||
osw.flush();
|
||||
}
|
||||
// 读取返回内容
|
||||
resultBuffer = new StringBuffer();
|
||||
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
|
||||
if (contentLength > 0) {
|
||||
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
|
||||
String temp;
|
||||
while ((temp = br.readLine()) != null) {
|
||||
resultBuffer.append(temp);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (osw != null) {
|
||||
try {
|
||||
osw.close();
|
||||
} catch (IOException e) {
|
||||
osw = null;
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (con != null) {
|
||||
con.disconnect();
|
||||
con = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (br != null) {
|
||||
try {
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
br = null;
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (con != null) {
|
||||
con.disconnect();
|
||||
con = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultBuffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description:使用HttpURLConnection发送delete请求
|
||||
*/
|
||||
public static String sendHttpURLDel(String urlParam, Map<String, Object> params, String charset, int timeout) {
|
||||
StringBuffer resultBuffer = null;
|
||||
// 构建请求参数
|
||||
StringBuffer sbParams = new StringBuffer();
|
||||
if (params != null && params.size() > 0) {
|
||||
for (Entry<String, Object> e : params.entrySet()) {
|
||||
sbParams.append(e.getKey());
|
||||
sbParams.append("=");
|
||||
sbParams.append(e.getValue());
|
||||
sbParams.append("&");
|
||||
}
|
||||
}
|
||||
HttpURLConnection con = null;
|
||||
OutputStreamWriter osw = null;
|
||||
BufferedReader br = null;
|
||||
// 发送请求
|
||||
try {
|
||||
URL url = new URL(urlParam);
|
||||
con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("DELETE");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
if (sbParams != null && sbParams.length() > 0) {
|
||||
osw = new OutputStreamWriter(con.getOutputStream(), charset);
|
||||
|
@ -133,8 +220,8 @@ public class HttpClientHelper {
|
|||
// 设置通用的请求属性
|
||||
con.setRequestProperty("accept", "*/*");
|
||||
con.setRequestProperty("connection", "Keep-Alive");
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
// 发送POST请求必须设置如下两行
|
||||
con.setDoOutput(true);
|
||||
|
@ -205,7 +292,8 @@ public class HttpClientHelper {
|
|||
url = new URL(urlParam);
|
||||
}
|
||||
con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
con.connect();
|
||||
InputStream is = con.getInputStream();
|
||||
|
@ -272,8 +360,11 @@ public class HttpClientHelper {
|
|||
} else {
|
||||
url = new URL(urlParam);
|
||||
}
|
||||
//String author = "Basic " + Base64.encode(("admin" + ":" + "123456").getBytes());
|
||||
con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
con.connect();
|
||||
resultBuffer = new StringBuffer();
|
||||
|
@ -329,8 +420,8 @@ public class HttpClientHelper {
|
|||
// 设置请求属性
|
||||
con.setRequestProperty("accept", "*/*");
|
||||
con.setRequestProperty("connection", "Keep-Alive");
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Authorization", author);
|
||||
con.setConnectTimeout(timeout*1000);
|
||||
// 建立连接
|
||||
con.connect();
|
||||
|
@ -360,27 +451,26 @@ public class HttpClientHelper {
|
|||
*/
|
||||
public static String httpClientPost(String urlParam, Map<String, Object> params, String charset) {
|
||||
StringBuffer resultBuffer = null;
|
||||
@SuppressWarnings({ "deprecation", "resource" })
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
//HttpClient client = new HttpClient();
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost(urlParam);
|
||||
httpPost.setHeader("Authorization", author);
|
||||
httpPost.setHeader("Content-Type", "application/json");
|
||||
|
||||
// 构建请求参数
|
||||
List<NameValuePair> list = new ArrayList<NameValuePair>();
|
||||
Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Entry<String, Object> elem = iterator.next();
|
||||
list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
|
||||
}
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
if (list.size() > 0) {
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
|
||||
if(params.size()>0){
|
||||
JSONObject jsonObject = JSONObject.fromObject(params);
|
||||
System.out.println(jsonObject.toString());
|
||||
StringEntity entity = new StringEntity(jsonObject.toString(),charset);
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
HttpResponse response = client.execute(httpPost);
|
||||
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
|
||||
// 读取服务器响应数据
|
||||
resultBuffer = new StringBuffer();
|
||||
|
||||
//br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
||||
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
|
||||
String temp;
|
||||
while ((temp = br.readLine()) != null) {
|
||||
|
@ -398,7 +488,6 @@ public class HttpClientHelper {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultBuffer.toString();
|
||||
}
|
||||
|
||||
|
@ -407,8 +496,9 @@ public class HttpClientHelper {
|
|||
*/
|
||||
public static String httpClientGet(String urlParam, Map<String, Object> params, String charset) {
|
||||
StringBuffer resultBuffer = null;
|
||||
@SuppressWarnings({ "deprecation", "resource" })
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
//@SuppressWarnings({ "deprecation", "resource" })
|
||||
//HttpClient client = new HttpClient();
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
BufferedReader br = null;
|
||||
// 构建请求参数
|
||||
StringBuffer sbParams = new StringBuffer();
|
||||
|
@ -428,10 +518,12 @@ public class HttpClientHelper {
|
|||
urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1);
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(urlParam);
|
||||
httpGet.setHeader("Authorization", author);
|
||||
httpGet.setHeader("Content-Type", "application/json");
|
||||
try {
|
||||
HttpResponse response = client.execute(httpGet);
|
||||
HttpResponse response = httpclient.execute(httpGet);
|
||||
// 读取服务器响应数据
|
||||
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),charset));
|
||||
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
|
||||
String temp;
|
||||
resultBuffer = new StringBuffer();
|
||||
while ((temp = br.readLine()) != null) {
|
||||
|
|
|
@ -71,6 +71,14 @@ public class InvokeMethod {
|
|||
.loadJSON("/projectTemplateParams/cgetParamsByTemplate.do?templateid=" + templateidstr);
|
||||
JSONObject jsonptpObject = JSONObject.fromObject(httpptp.toString());
|
||||
JSONArray jsonarr = JSONArray.fromObject(jsonptpObject.getString("params"));
|
||||
for(int i=0;i<jsonarr.size();i++){
|
||||
JSONObject tempobj=(JSONObject) jsonarr.get(i);
|
||||
String str=tempobj.get("param").toString();
|
||||
if("[".equals(str.substring(0, 1))&&"]".equals(str.substring(str.length()-1))){
|
||||
tempobj.element("param", "***"+str);
|
||||
jsonarr.set(i, tempobj);
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
|
||||
new JsonConfig());
|
||||
|
@ -91,6 +99,9 @@ public class InvokeMethod {
|
|||
}
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
for (ProjectTemplateParams ptp : paramslist) {
|
||||
if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){
|
||||
ptp.setParam(ptp.getParam().substring(3));
|
||||
}
|
||||
params.put(ptp.getParamname().replaceAll(""", "\""), ptp.getParam().replaceAll(""", "\""));
|
||||
}
|
||||
|
||||
|
@ -136,6 +147,14 @@ public class InvokeMethod {
|
|||
.loadJSON("/projectTemplateParams/cgetParamsByTemplate.do?templateid=" + templateidstr);
|
||||
JSONObject jsonptpObject = JSONObject.fromObject(httpptp.toString());
|
||||
JSONArray jsonarr = JSONArray.fromObject(jsonptpObject.getString("params"));
|
||||
for(int i=0;i<jsonarr.size();i++){
|
||||
JSONObject tempobj=(JSONObject) jsonarr.get(i);
|
||||
String str=tempobj.get("param").toString();
|
||||
if("[".equals(str.substring(0, 1))&&"]".equals(str.substring(str.length()-1))){
|
||||
tempobj.element("param", "***"+str);
|
||||
jsonarr.set(i, tempobj);
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
|
||||
new JsonConfig());
|
||||
|
@ -156,7 +175,10 @@ public class InvokeMethod {
|
|||
}
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
for (ProjectTemplateParams ptp : paramslist) {
|
||||
params.put(ptp.getParamname(), ptp.getParam());
|
||||
if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){
|
||||
ptp.setParam(ptp.getParam().substring(3));
|
||||
}
|
||||
params.put(ptp.getParamname().replaceAll(""", "\""), ptp.getParam().replaceAll(""", "\""));
|
||||
}
|
||||
|
||||
if (functionname.toLowerCase().equals("socketpost")) {
|
||||
|
@ -218,5 +240,4 @@ public class InvokeMethod {
|
|||
System.out.println(key);
|
||||
System.out.println(value);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue