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
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.