Commit c2745fc8 by shuai

20190130

parent 702705cd
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<classes> <classes>
<class name="com.puhui.test.RenMai_APITest"> <class name="com.puhui.test.RenMai_APITest">
<methods> <methods>
<include name="f" invocation-numbers="0 1 2 3 4 5 6 7 8 "/> <include name="f" invocation-numbers="0 1 2 3 4 5 6 "/>
</methods> </methods>
</class> </class>
</classes> </classes>
......
package com.offcn.TestUnti;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static void main(String[] args) {
String s=HttpRequest.sendGet("http://beta.alitest.eoffcn.com/template/addStuden/sign/5d114486999208096dace001ae4bc45a",
"user_info=[{\"package_id\":391634,\"username\":\"aaa\",\"phone\":15656333337,\"sso_id\":1}]&timestamp=1548828632&appid=jiaowu");
try {
String s1=new String(s.getBytes(),"utf-8");
System.out.println(s1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
// String urlNameString = url ;
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NSwicGhvbmUiOiIxODUxMTg1MjA0OSIsImlhdCI6MTQ4NzEzMjU0OX0.2aZ8FcfE52CYpXc4VUYjdfzzLzXwVOE-J8CnMlhUPic");
connection.setRequestProperty("Content-Type", "application/json");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// in = new BufferedReader(new InputStreamReader(
// new FileInputStream("c:ccc.txt"),"utf-8"));
// OutputStreamWriter o=new OutputStreamWriter(new FileOutputStream("c:aaa.txt"),"utf-8");
// o.write("11平台官网——打造最好的游戏平台1\r\n"+
// "11平台官网——打造最好的游戏平台2\r\n"+
// "11平台官网——打造最好的游戏平台3\r\n");
// o.close();
// String sss=new InputStreamReader(
// connection.getInputStream()).getEncoding();
// System.out.println(sss+"用这个编码的");
String line;
while ((line = in.readLine()) != null) {
// o.write(line+"\r\n");
result += line;
result += "\r\n";
}
// o.close();
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NSwicGhvbmUiOiIxODUxMTg1MjA0OSIsImlhdCI6MTQ4NzEzMjU0OX0.2aZ8FcfE52CYpXc4VUYjdfzzLzXwVOE-J8CnMlhUPic");
conn.setRequestProperty("Content-Type", "application/json");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
result += "\r\n";
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}
\ No newline at end of file
...@@ -665,8 +665,7 @@ public class RequestDataUtils { ...@@ -665,8 +665,7 @@ public class RequestDataUtils {
} }
} }
return re; return re;
} }
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
public static Response Get_one_cookie_pre(HashMap<String, Object> data, public static Response Get_one_cookie_pre(HashMap<String, Object> data,
...@@ -837,4 +836,5 @@ public class RequestDataUtils { ...@@ -837,4 +836,5 @@ public class RequestDataUtils {
} }
return re; return re;
} }
} }
...@@ -4,6 +4,7 @@ package com.offcn.api.nwn.service; ...@@ -4,6 +4,7 @@ package com.offcn.api.nwn.service;
import io.restassured.path.json.JsonPath; import io.restassured.path.json.JsonPath;
import io.restassured.response.Response; import io.restassured.response.Response;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
...@@ -12,9 +13,8 @@ import java.util.Date; ...@@ -12,9 +13,8 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import com.offcn.TestUnti.HttpRequest;
import com.offcn.TestUnti.Log; import com.offcn.TestUnti.Log;
import com.offcn.TestUnti.MapUtil; import com.offcn.TestUnti.MapUtil;
import com.offcn.TestUnti.RequestDataUtils; import com.offcn.TestUnti.RequestDataUtils;
...@@ -24,7 +24,6 @@ import com.offcn.process.NWN; ...@@ -24,7 +24,6 @@ import com.offcn.process.NWN;
import com.offcn.process.TK; import com.offcn.process.TK;
import com.offcn.TestUnti.ListUtil; import com.offcn.TestUnti.ListUtil;
import net.sf.json.JSONObject;
...@@ -40,6 +39,7 @@ public class addStuden extends NWN implements API { ...@@ -40,6 +39,7 @@ public class addStuden extends NWN implements API {
public String template_id_1;//母板ID public String template_id_1;//母板ID
public String response;//返回结果
// public String phone;//层级包id // public String phone;//层级包id
@Override @Override
public void initialize(HashMap<String, Object> data) { public void initialize(HashMap<String, Object> data) {
...@@ -76,20 +76,27 @@ public class addStuden extends NWN implements API { ...@@ -76,20 +76,27 @@ public class addStuden extends NWN implements API {
// Map<String,String> m=new HashMap<String,String>(); // Map<String,String> m=new HashMap<String,String>();
// m.put("user_info", parameter); // m.put("user_info", parameter);
//Response re = RequestDataUtils.Post_cooike_form_data(data, Url,"PHPSESSID",PHPSESSID,m); //Response re = RequestDataUtils.Post_cooike_form_data(data, Url,"PHPSESSID",PHPSESSID,m);
Response re = RequestDataUtils.Get_one_cookie_pre(data, Url, "PHPSESSID",PHPSESSID); // Response re = RequestDataUtils.Get_one_cookie_pre(data, Url, "PHPSESSID",PHPSESSID);
//Response re = RequestDataUtils.Get_one_cookie(data, serviceURL, cookie1Name, cookie1value) //只能无参
// Response re = RequestDataUtils.Get_token(data, Url, ""); response=HttpRequest.sendGet("http://beta.alitest.eoffcn.com/template/addStuden/sign/5d114486999208096dace001ae4bc45a",
return re; "user_info=[{\"package_id\":391634,\"username\":\"aaa\",\"phone\":15656333337,\"sso_id\":1}]&timestamp=1548828632&appid=jiaowu");
try {
String s1=new String(response.getBytes(),"utf-8");
System.out.println(s1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
} }
@Override @Override
public String handleOutput(Response re, HashMap<String, Object> data) { public String handleOutput(Response re, HashMap<String, Object> data) {
JsonPath jp = re.body().jsonPath(); JsonPath jp = new JsonPath(response);
System.out.println( "jp===="+ jp); System.out.println( "jp===="+ jp);
boolean result = true; boolean result = true;
String failReason = ""; String failReason = "";
String json = re.asString(); String json = response;
System.out.println("response=========="+StringUtils.decodeUnicode(json)); System.out.println("response=========="+StringUtils.decodeUnicode(json));
if ((data.get("statusCode") != null) if ((data.get("statusCode") != null)
......
...@@ -99,13 +99,18 @@ public class APITest_nwn extends NWN{ ...@@ -99,13 +99,18 @@ public class APITest_nwn extends NWN{
if(data.get("Description").toString().contains("流程")){ if(data.get("Description").toString().contains("流程")){
result = obj.handleOutput(re, data); result = obj.handleOutput(re, data);
} }
}else{
result = obj.handleOutput(re, data);
} }
codeORerrcode=getCode(re); // else{
msgORerrmsy=getMsg(re); // result = obj.handleOutput(re, data);
// }
// codeORerrcode=getCode(re);
// msgORerrmsy=getMsg(re);
} }
result = obj.handleOutput(re, data);
codeORerrcode=getCode(re);
msgORerrmsy=getMsg(re);
Log.logInfo("返回结果="+StringUtils.decodeUnicode(body)); Log.logInfo("返回结果="+StringUtils.decodeUnicode(body));
System.out.println(); System.out.println();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment