curl与java结合传入get.post,head请求,
比如: curl的地址:
curl -l 127.0.0.1:8080/spacobj/core/do?acid=100 -H "token:101hh" -H "Content-type: application/json" -X POST -d {"telNum":"13521389587","pass":"‘my12345‘","nick":"‘明天会更好‘","sms":"‘7864AQ‘"}
注意 json格式中的字符串必须是"‘ 里面是字符串‘"
java中获取的代码如下:
@RequestMapping(value = "do", method = RequestMethod.POST)
public String mydo(
@RequestParam(value = "acid", required = false, defaultValue = "0") int acid,
@RequestHeader(value = "token", required = false, defaultValue = "0") String token,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException, IOException, ParseException {
//下面创建一个对象,用对象来接受json传过来的数据
T_Account tac = new T_Account();
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(
request.getInputStream(), "UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
String json = sb.toString();
JSONObject jsonObject = JSONObject.fromObject(json);
Iterator ite = jsonObject.keys();
// 遍历jsonObject数据,用对象来接收。通过key的判断,是那个对象的字段来接受post中json请求过来的
while (ite.hasNext()) {
String key = ite.next().toString();
if (key.equals("telNum")) {
tac.setTelNum(jsonObject.get(key).toString());
}
if (key.equals("pass")) {
tac.setPass(jsonObject.get(key).toString());
}
if (key.equals("nick")) {
tac.setNick(jsonObject.get(key).toString());
}
if (key.equals("sms")) {
tac.setSms(jsonObject.get(key).toString());
}
String value = jsonObject.get(key).toString();
}
}