yeelink HTTP格式学习(二)

//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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值