我想使用WiServer库从Black Widow 1.0板(带Wi-Fi)调用REST服务。
HTTP POST应如下所示:
http://sensor.zonosoft.com/SensorService.svc/SaveMeasurement HTTP/1.0
Content-Type: application/json; charset=utf-8
{"Id":0,"Version":0,"Name":"Toilet 1","Sensors":[{"Id":0,"Version":0,"Measurements":[{"Time":"\/Date(1295820124154+0100)\/","Value":1}],"Name":"Dor"},{"Id":0,"Version":0,"Measurements":[{"Time":"\/Date(1295820124155+0100)\/","Value":1}],"Name":"Tonden"}]}
当我用Fiddler2的Request Builder测试POST请求时,Fiddler将这两行添加到标题中。
Host: sensor.zonosoft.com
Content-Length: 257
and Fiddler detects the response:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 10
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=unstlzfj43ug2xrdyua5x2fk; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Tue, 25 Jan 2011 14:33:10 GMT
Connection: close
"Success!"
这就是它应该如何运作。
这是我使用POSTrequest的Arduino sketch。我已经尝试了很多东西来实现它,但这是我能想到的最好的东西。 不幸的是,它不会生成正确的POST消息。原因是什么?
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"__yourSSID__"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"__yourPassword__"}; // max 64 characters
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
void printData(char* data, int len)
{
Serial.println("printData");
// Print the data returned by the server
// Note that the data is not null-terminated, may be broken up into smaller packets, and
// includes the HTTP header.
while (len-- > 0) {
Serial.print(*(data++));
}
}
uint8 ip[] = {195,128,175,40};
POSTrequest sendInfo(
ip,
80,
"sensor.zonosoft.com\nContent-Type: application/json; charset=utf-8",
"/SensorService.svc/SaveMeasurement",
createBody);
void setup() {
WiServer.init(NULL);
Serial.begin(9600);
WiServer.enableVerboseMode(true);
sendInfo.setReturnFunc(printData);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void createBody()
{
char json[] = "{\"Id\":0,\"Version\":0,\"Name\":\"Toilet 2\",\"Sensors\":[{\"Id\":0,\"Version\":0,\"Measurements\":[{\"Time\":\"\\/Date(1295820124154+0100)\\/\",\"Value\":1}],\"Name\":\"Dor\"},{\"Id\":0,\"Version\":0,\"Measurements\":[{\"Time\":\"\\/Date(1295820124155+0100)\\/\",\"Value\":1}],\"Name\":\"Tonden\"}]}";
WiServer.print(json);
}
void loop()
{
// Check if it's time to get an update
if (millis() >= updateTime)
{
Serial.println("Start POST");
sendInfo.submit();
Serial.println("End POST");
updateTime += 1000 * 30;
Serial.println(updateTime);
}
WiServer.server_task();
delay(10);
}
答案 0 :(得分:1)
我使用Wireshark设置了HTTP跟踪,发现WiServer添加了自己的硬编码Content-Type: application/x-www-form-urlencoded
。因此Content-Type
在标题中出现了两次。
我已在application/json; charset=utf-8
中将硬编码内容类型更改为WiShield\strings.c
。我从主机名中删除了内容类型。
有效!