HTTP(S)请求利器

2019-10-23 19:32:00     

kevinsawicki是一个小巧又便捷的HTTP(S)请求利器。有时,如果只是向做快捷简易的HTTP(S)请求,那么kevinsawicki将会是一种不错的选择。

前期准备:http-request-6.0.jar

<!-- https://mvnrepository.com/artifact/com.github.kevinsawicki/http-request -->
<dependency>
    <groupId>com.github.kevinsawicki</groupId>
    <artifactId>http-request</artifactId>
    <version>6.0</version>
</dependency>
  • POST传输json请求体
@RequestMapping(value = "/hpkapi")
public class HpkApiController {
	@RequestMapping(value = { "/invokeHpkApi" }, method = { RequestMethod.POST }, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String invokeHpkApi(@RequestBody JSONObject param) throws IOException {
		return "Bounds" + param.getString("orderId");
	}
}
public static void main(String args[]) {
		JSONObject JB = new JSONObject();
		JB.put("orderId", "62598313");
		HttpRequest httpRequest = new HttpRequest("http://localhost:7071/hpkapi/invokeHpkApi", "POST");
		httpRequest.header("Content-Type", "application/json");
		httpRequest.send(JB.toString());
		String res = httpRequest.body();
		System.out.println(res);
	}
  • POST传输form表单
@RequestMapping(value = "/hpkapi")
public class HpkApiController {
	@RequestMapping(value = { "/invokeHpkApi" }, method = { RequestMethod.POST }, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String invokeHpkApi(@RequestParam("orderId") String orderId, String orderName) throws IOException {
		return "Bounds" + orderId + orderName;
	}
}
public static void main(String args[]) {
		Map<String, Object> params = new HashMap<>();
		params.put("orderId", "62598313");
		params.put("orderName", "手机");
		HttpRequest httpRequest = new HttpRequest("http://localhost:7071/hpkapi/invokeHpkApi", "POST");
		httpRequest.contentType("application/json", "UTF-8");
		httpRequest.form(params);
		String res = httpRequest.body();
		System.out.println(res);
	}
  • 字符串包含Unicode转码成中文
/**
	 * @param theString
	 * @return 将字符串包含Unicode转码成中文
	 */
	public static String decodeUnicode(String theString) {
		char aChar;
		int len = theString.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for (int x = 0; x < len;) {
			aChar = theString.charAt(x++);
			if (aChar == '\\') {
				aChar = theString.charAt(x++);
				if (aChar == 'u') {
					// Read the xxxx
					int value = 0;
					for (int i = 0; i < 4; i++) {
						aChar = theString.charAt(x++);
						switch (aChar) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';
					else if (aChar == 'n')
						aChar = '\n';
					else if (aChar == 'f')
						aChar = '\f';
					outBuffer.append(aChar);
				}
			} else
				outBuffer.append(aChar);
		}
		return outBuffer.toString();
	}
Java解析XML文本

XML是一种通用的数据交换格式,它的平台无关性、语言无关性、系统无关性、给数据集成与交互带来了极大的方便。XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已。

规范代码

当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失,因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。

 发表评论