问题描述
我正在用Java构建一个应用程序,以等待服务器的请求。
应用程序在端口6868上启动新的服务器侦听器,并等待JSON格式的请求正文
@H_502_5@private static void startListening() { try (ServerSocket serverSocket = new ServerSocket(6868)) { System.out.println("Server is listening on port " + 6868); while (!done) { Socket socket = serverSocket.accept(); //Accepting the request InputStream input = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); Scanner sc = new Scanner(new InputStreamReader(input)); //Get the request and convert to a string String inline = ""; while(sc.hasNext()) { inline = inline + sc.nextLine(); } System.out.println("JSON in data context:"); //Print the JSON request System.out.println(inline); sc.close(); } } catch (IOException e) { e.printstacktrace(); } }
我在这里有两个问题:
- 当我打印结果时,我得到了:
@H_502_5@POST / HTTP/1.1Host: localhost:6868User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:79.0) Gecko/20100101 Firefox/79.0Accept: */*Accept-Language: es-MX,es;q=0.8,en-US;q=0.5,en;q=0.3Accept-Encoding: gzip,deflateContent-Type: text/plain;charset=UTF-8Content-Length: 759Origin: http://localhost:4200Connection: keep-aliveReferer: http://localhost:4200/cliente/adm/fiscalprinter{"requests":[{"cmd":2}],"modelo":27,"puerto":7,"baudios":9600}
如您所见,我不仅在json中获取请求的主体,还获取与来自服务器的服务器相关的所有数据。
我该怎么做才能仅获取请求的json正文?
- 如何将响应发送到即将发出此请求的服务器?因为服务器正在等待永远不会收到的响应...
希望您能帮助我,谢谢!
编辑:
这是打字稿angular的函数,它将请求发送到我的Java应用程序:
@H_502_5@host = "localhost"; http = new XMLHttpRequest(); async sendData(request) { var url = null; url = "http://" + this.host + ":6868"; this.http.open("POST",url,false); this.http.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var resultado = JSON.parse(this.responseText); var errorCode = resultado.result.code; var errorDesc = resultado.result.description; var lastResponse = null; if (typeof(resultado.description) != 'undefined'){ lastResponse = resultado; } else { lastResponse = null; } } }; var stringData = JSON.stringify(request); this.http.send(stringData); return this.lastResponse; }
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)