HttpURLConnection在POST请求时我想传递jeson字符串,如何传递啊!
人生最曼妙的风景,竟是内心的淡定与从容!
String json = "{'goods':'iphone'}"; String btnSearch = "EMS快递查询"; URL url = new URL("http://www.kd185.com/ems.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");// 提交模式 // conn.setConnectTimeout(10000);//连接超时 单位毫秒 // conn.setReadTimeout(2000);//读取超时 单位毫秒 conn.setDoOutput(true);// 是否输入参数 StringBuffer params = new StringBuffer(); params.append("goods").append("=").append(json).append("&") .append("btnSearch").append("=").append(btnSearch); byte[] bypes = params.toString().getBytes(); conn.getOutputStream().write(bypes);// 输入参数 InputStream inStream=conn.getInputStream(); System.out.println(new String(StreamTool.readInputStream(inStream), "gbk"));
人生最曼妙的风景,竟是内心的淡定与从容!