//JAVA代码写的yeelink POST/GET测试程序:
package HTTPTEST;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MAIN {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
String host_name = "api.yeelink.net";
// HTTP请求 http://api.yeelink.net/v1.1/device/15527/sensor/26738/
datapoints
String send_buf = "GET /v1.1/device/15527/sensor/26738/datapoints HTTP/1.
1\r\nU-ApiKey: 29f95415ec8138c351c13b6dce6e80ee\r\nHost: api.yeelink.net\r\n\r
\n";
//String post_buf = "GET /v1.1/device/15527/sensor/26738/datapoints HTTP/
1.1\r\nU-ApiKey: 29f95415ec8138c351c13b6dce6e80ee\r\nHost: api.yeelink.net\r\n
\r\n";
String post_buf = "POST /v1.1/device/15527/sensor/26738/datapoints HTTP/1
.1\r\nHost: api.yeelink.net\r\nAccept:*/*\r\nU-ApiKey:
29f95415ec8138c351c13b6dce6e80ee\r\n";
Socket socket;
BufferedReader in;
PrintWriter out;
char[] cbuf = null;
cbuf = new char[1024];
InetAddress myServer = null;
//DNS解析出IP
myServer = InetAddress.getByName(host_name);
System.out.println(myServer);
System.out.println("---------------->get start");
//创建套接字
socket = new Socket(myServer, 80);
//创建套接字读写流
in = new BufferedReader(new InputStreamReader(socket.getInputStream())
);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
//发送请求
out.println(send_buf);
System.out.print(send_buf);
//读服务器返回
in.read(cbuf);
System.out.println(cbuf);
System.out.println("get end");
socket.close();
System.out.println("---------------->get end");
Thread.sleep(1000);
System.out.println("---------------->post start");
socket = new Socket(myServer, 80);
//创建套接字读写流
in = new BufferedReader(new InputStreamReader(socket.getInputStream())
);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
out.print(post_buf);
System.out.print(post_buf);
out.print("Content-Length:");
System.out.print("Content-Length:");
String value = "{\"value\":3}";
out.print(value.length());
System.out.print(value.length());
out.print("\r\n\r\n");
System.out.print("\r\n");
out.print(value);
out.println();
System.out.println(value);
//读服务器返回
in.read(cbuf);
System.out.println(cbuf);
System.out.println("---------------->post end");
socket.close();
}
}
测试结果:api.yeelink.net/42.96.164.52
---------------->get start
GET /v1.1/device/15527/sensor/26738/datapoints HTTP/1.1
U-ApiKey: 29f95415ec8138c351c13b6dce6e80ee
Host: api.yeelink.net
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Sat, 08 Nov 2014 18:00:02 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.10-1ubuntu3.6
55
{"value":5,"timestamp":"2014-11-09T01:57:15","sensor_id":"26738","device_id":"15527"}
0
get end
---------------->get end
---------------->post start
POST /v1.1/device/15527/sensor/26738/datapoints HTTP/1.1
Host: api.yeelink.net
Accept:*/*
U-ApiKey: 29f95415ec8138c351c13b6dce6e80ee
Content-Length:11
{"value":3}
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Sat, 08 Nov 2014 18:00:03 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Content-Length: 0
tu3.6
55
{"value":5,"timestamp":"2014-11-09T01:57:15","sensor_id":"26738","device_id":"15527"}
0
---------------->post end