通过HttpURLConnection调用接口

 1 URL realurl  = new URL(REQUESTURL); //REQUESTURL为常量,请求地址
 2 HttpURLConnection httpURLConnection = (HttpURLConnection) realurl.openConnection();
 3 httpURLConnection.setRequestMethod("POST");
 4 httpURLConnection.setRequestProperty("Content-type","application/json");
 5 httpURLConnection.setDoInput(true);
 6 httpURLConnection.setDoOutput(true);
 7 DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
 8 out.write(info.toString().getBytes("UTF-8")); //info为JSONObject类型,为请求参数,格式为{"entity":{"id":"123456","name":"levi"}}
 9 out.flush();
10 out.close();
11 InputStream inputStream = httpURLConnection.getInputStream();   
12 String byteArrayOutputStream = streamToString(inputStream);    //streamToString为一个转化方法,将inputStream 类型转为String类型
13 JSONObject result = JSONObject.parseObject(byteArrayOutputStream);

 

上一篇:落谷_导弹拦截


下一篇:Secure Cookie Attribute