try{
String host = 地址就不贴出来了;
int port = 8080;
int updateDelay = 1000;
Socket s = new Socket(host,port);
s.setSoTimeout(5000);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// PrintWriter buffW = new PrintWriter(s.getOutputStream());
BufferedWriter buffW = new BufferedWriter(new OutputStreamWriter(dos,StandardCharsets.UTF_8));
String data = "{\"service\":2,\"Id\":\"0\"}";
String postHeader =
"POST / HTTP/1.1\r\n" +
"Host:"+" 地址就不贴出来了\r\n" +
"Content-Type:application/json" +
"\r\n";
System.out.println(postHeader);
buffW.write(postHeader);
buffW.write(data);
buffW.write("\r\n");
buffW.flush();
s.shutdownOutput();
Thread.sleep(updateDelay);
DataInputStream dis = new DataInputStream(s.getInputStream());
byte[] recvStr = new byte[4096];
dis.read(recvStr);
System.out.println(new String(recvStr,StandardCharsets.UTF_8));
buffW.close();
dos.close();
//switch
} catch (IOException e) {
e.printStackTrace();
System.out.println("请求错误!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
返回的结果一直是:
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close