Rumah > Soal Jawab > teks badan
HttpURLConnection在POST请求时我想传递jeson字符串,如何传递啊!
PHPz2017-04-17 17:50:05
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"));