CmdHttpCaller { public static void main(String[] args) { CommandLineParser parser = new DefaultParser(); Boolean isDebug = false; try { CommandLine commandline = parser.parse(opt, args); if (commandline.getOptions().length == 0 && commandline.getArgs().length > 0) { CmdCaller.main(args); return; } if (commandline.hasOption("h")) { usage(null); return; } String ak = commandline.getOptionValue("ak"); String sk = commandline.getOptionValue("sk"); String api = commandline.getOptionValue("api"); String version = commandline.getOptionValue("version"); Boolean sdkv = commandline.hasOption("sdkv"); String method = commandline.getOptionValue("method"); String[] headers = commandline.getOptionValues("H"); String[] params = commandline.getOptionValues("D"); String url = commandline.getOptionValue("url"); String proxy = commandline.getOptionValue("proxy"); String cbJSON = commandline.getOptionValue("cbJSON"); boolean nonce = commandline.hasOption("nonce"); String bizIdKey = commandline.getOptionValue("bizIdKey"); String bizId = commandline.getOptionValue("bizId"); isDebug = commandline.hasOption("d"); if (sdkv) { Version.version(); return; } String signImpl = commandline.getOptionValue("sign"); String verifySignImpl = commandline.getOptionValue("verify"); if (isDebug) { System.out.println("url=" + url); System.out.println("api=" + api); System.out.println("version=" + version); System.out.println("bizIdKey=" + bizIdKey); System.out.println("bizId=" + bizId); System.out.println("ak=" + ak); System.out.println("sk=" + sk); System.out.println("proxy=" + proxy); System.out.println("nonce=" + nonce); System.out.println("signImpl=" + signImpl); System.out.println("verifySignImpl=" + verifySignImpl); printKV("HTTP Headers", headers); printKV("HTTP Params", params); } if (isEmpty(api)) { usage(MessageHelper.getMessage("cli.defparam", "-api")); return; } if (isEmpty(version)) { usage(MessageHelper.getMessage("cli.defparam", "-version")); return; } if (isEmpty(url)) { usage(MessageHelper.getMessage("cli.defparam", "-url")); return; } if (method == null) { method = "get"; } HttpParameters.Builder builder = HttpParameters.newBuilder(); if (bizIdKey != null && !bizIdKey.trim().equals("")) { HttpCaller.bizIdKey(bizIdKey); } builder.api(api).version(version).method(method).bizId(bizId).requestURL(url).accessKey(ak).secretKey(sk).signImpl(signImpl).verifySignImpl(verifySignImpl); if (headers != null) { for (String header : headers) { String[] kv = header.split(":", 2); if (kv == null || kv.length != 2) { System.out.println("" + header); return; } builder.putHeaderParamsMap(kv[0], kv[1]); } } if (params != null) { for (String param : params) { String[] kv = param.split("=", 2); if (kv == null || kv.length != 2) { System.out.println(MessageHelper.getMessage("cli.defh", param)); return; } builder.putParamsMap(kv[0], kv[1]); } } if (cbJSON != null) { if ("cget".equalsIgnoreCase(method) || "get".equalsIgnoreCase(method)) { System.out.println(MessageHelper.getMessage("cli.defpost")); return; } if (cbJSON.startsWith("'")) { System.out.println(MessageHelper.getMessage("cli.json.prefix")); return; } builder.contentBody(new ContentBody(cbJSON)); } builder.nonce(nonce); if (isDebug) { builder.diagnostic(true); } boolean curlOnly = false; if (method.toLowerCase().startsWith("c")) { curlOnly = true; HttpCaller.setCurlResponse(true); } StringBuffer resHttpHeaders = new StringBuffer(); if (proxy != null) { String errMsg = MessageHelper.getMessage("cli.errproxy", proxy); String[] pcs = proxy.split(":"); if (pcs == null || pcs.length != 2) { System.out.println(errMsg); return; } try { HttpCaller.setProxyHost(pcs[0], Integer.parseInt(pcs[1]), null); } catch (Exception e) { System.out.println(errMsg); return; } } HttpReturn ret = HttpCaller.invokeReturn(builder.build()); if (curlOnly) { System.out.println("---- curlString = " + ret.getResponseStr()); } else { if (isDebug) { System.out.println("Diagnostic Info:" + ret.diagnosticInfo); } System.out.println("---- response http headers = " + ret.responseHeaders); System.out.println("---- retStr = " + ret.getResponseStr()); int times = Integer.getInteger("test.stress.times", 0); for (int i = 2; i <= times; i++) { ret = HttpCaller.invokeReturn(builder.build()); System.out.println("---- retStr [#" + i + "] = " + ret.getResponseStr()); } } } catch (Exception e) { System.out.println("-- operation error：" + e.getMessage()); e.printStackTrace(System.out); } }  static void main(String[] args); static Options opt; }
@Test public void testCGet() { CmdHttpCaller.main(new String[]{"-url", "http: "-api", "aaa", "-version", "1.0.0", "-method", "cget", "-ak", "ak", "-sk", "sk", "-H", "name1:abc1", "-D", "param1=value1", "-sign", "com.alibaba.csb.sdk.SampleSignImpl", "-verify", "com.alibaba.aosp.extension.security.SampleVerifySignImpl", "-nonce"}); }