- 首先要有一个发送的地址
- 通过URL获取到连接
- 设置连接的参数
- 向连接发送数据
- 接收返回的数据
public static void main(String[] args) throws IOException {
String url = "https://wxcms-bg.ihxlife.com/selfmanage/xq/vnc/call/bind";
Map<String, String> map = new HashMap<>();
map.put("operatorCode", "123344");
map.put("customerCode", "123344");
map.put("telA", "15210896450");
map.put("telB", "15210896466");
Mybean mybean = new Mybean();
mybean.setCustomerCode("123344");
mybean.setOperatorCode("123344");
mybean.setTelA("15210896450");
mybean.setTelB("15210896466");
String jsonString = JSONObject.toJSONString(map);
System.out.println("jsonString===>"+jsonString);
URL murl = new URL(url);
HttpURLConnection conn=null;
conn= (HttpURLConnection) murl.openConnection();
conn.setConnectTimeout(6000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Charset","UTF-8");
conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
conn.setRequestProperty("accept","application/json");
byte[] bytes = jsonString.getBytes();
conn.setRequestProperty("Content-Length",String.valueOf(bytes.length));
OutputStream outwrite = conn.getOutputStream();
outwrite.write(bytes);
outwrite.flush();
outwrite.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
conn.disconnect();
System.out.println(sb.toString());
JSONObject jsonObject = JSON.parseObject(sb.toString());
String o = (String) jsonObject.get("VirtualTel");
System.out.println("VirtualTel===>"+o);
}