????????????????????????????????????????????????????????????????httpЭ?鷢????????????????????????????????????????
???????????????????????????????????????????????????????????????????????????
??????????????????????????????NTS????????????λ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????????
????1???????в??????????????????????
????2????HttpServer??????????
????3???????HttpServer??????ó????????
????4??HttpServer?????????????????????????????????????ж???
????5?????HttpServer
????????????????????????????????
????????????????????????????????????????????????á?
public static int check(int port) {
int _port;
try {
ServerSocket sock = new ServerSocket(port);
sock.close();
_port = port;
System.out.println("Port : " + port + " is Ok");
} catch (IOException e) {
System.out.println("Port : " + port + " is occupied ??Try to port : " + (port + 1));
_port = port + 1;
check(_port);
}
return _port;
}
????????String addrip = InetAddress.getLocalHost().getHostAddress();  ???????IP??????????????HttpServer?????????????
??????????HttpServer?????????
public static String callbackHttpServer(int minPort) throws IOException {
int port;
String addrip = InetAddress.getLocalHost().getHostAddress();
port = check(minPort);
String ipAddress = addrip + ":" + port;
System.out.println("address is " + ipAddress);
InetSocketAddress addr = new InetSocketAddress(port);
server = HttpServer.create(addr?? 0);
MyHandler myHandler=new MyHandler();
server.createContext("/"?? myHandler);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port : " + port);
return ipAddress;
}
????????????HttpServer????????
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("POST")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type"?? "text/plain");
exchange.sendResponseHeaders(200?? 0);            //?????????????200?????????????????????????
InputStream requestBody = exchange.getRequestBody();          //??????????
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();           //????post?????header???????????????
String body = slurp(requestBody?? 1024);          //??InputStream ????????String????
JsonObject messageBody = new JsonParser().parse(body).getAsJsonObject();
reparam=messageBody;
this.setReparam(reparam);
System.out.println("request body is >>>>>:" + this.getReparam());    //???????????????????
requestBody.close();
}
}
??????????HttpServer??
?????????????????????????δ???????????????HttpServer????
public JsonObject getCallbackBody()throws IOException?? InterruptedException
{
int i=0;
while (true) {
if(MyHandler.getReparam()!=null||i++>30)
break;
else
TimeUnit.SECONDS.sleep(5);
}
String type=MyHandler.judgeCallbackType(MyHandler.getReparam());
System.out.println("type is "+type);
JsonObject param=MyHandler.getReparam();
System.out.println("jsonObject is : "+param.toString());
MyHttpServer.stopHttpServer();
System.out.println("listen service is closed ");
return param;
}
}
?????????????????http??????????????????????????У?????????HttpServer????????????????telnet???????????????????????post??????????