首页 > 代码库 > aaaa
aaaa
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.net.URL;import java.net.URLConnection;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Iterator;import java.util.Map;import java.util.TreeMap;/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * *///import lrapi.lr;public class Actions { private static String appkey = "113800"; public static void main(String[] args) throws Throwable { new Actions().action(); } public int init() throws Throwable { return 0; }// end of init public int action() throws Throwable { // String url = "http://192.168.199.182:8080/api/device/scanQrCode"; String url = "http://192.168.199.182:8080/api/device/userInfo"; // 69faab6268350295550de7d587bc323d Map<String, Object> param = new TreeMap<String, Object>(); param.put("userId", "1"); String sign = getSign(param); param.put("sign", sign); // /repairViewById?id=11&sign=1 String result = HttpRequestUtil.sendPost(url, param); System.out.println(result); return 0; }// end of action public int end() throws Throwable { return 0; }// end of end public static String getSign(Map<String, Object> map) { StringBuffer sb = new StringBuffer(appkey); Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> ety = it.next(); sb.append(ety.getKey() + "=" + ety.getValue()); } sb.append(appkey); // System.out.println(sb); return md5(sb.toString()); } public static String md5(String str) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(str.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException caught!"); System.exit(-1); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append( Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.toString(); } static class HttpRequestUtil { private static final String AND = "&"; private static final String EQUAL = "="; private static final String QUESTION = "?"; /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, Map<String, Object> param) { StringBuffer result = new StringBuffer(); BufferedReader in = null; try { String urlNameString = url + QUESTION + getParamString(param); URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection .setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 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())); String line; while ((line = in.readLine()) != null) { result.append(line); } } 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.toString(); } private static String getParamString(Map<String, Object> param) { if (null == param || 0 == param.keySet().size()) { return ""; } StringBuffer paramString = new StringBuffer(); Iterator<Map.Entry<String, Object>> iter = param.entrySet() .iterator(); while (iter.hasNext()) { Map.Entry<String, Object> entry = iter.next(); paramString.append(AND).append(entry.getKey()).append(EQUAL) .append(entry.getValue()); } paramString = paramString.deleteCharAt(0); return paramString.toString(); } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, Map<String, Object> param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(getParamString(param)); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } 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; } }}
aaaa
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。