开发者

Problem creating a HTTP POST message with the wiserver-library

I would like to call a REST-service from a Black Widow 1.0 board (with Wi-Fi) using the WiServer library.

The HTTP POST should look like this:

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"}]}

When I test the POST request with Fiddler2's Request Builder, Fiddler adds these two lines to the header.

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!"

That is how it's supposed to work.

Here is my Arduino sketch where I use POSTrequest. I have tried a lot of things to get it working, but this is the best I can come up with. Unfortunately it won't generate the correct POST message. What could the reason be?

#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) {
 开发者_JS百科   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);
}


I have set up an HTTP trace using Wireshark, and found that WiServer adds its own hardcoded Content-Type: application/x-www-form-urlencoded. So the Content-Type appeared twice in the header.

I have changed the hardcoded content type to application/json; charset=utf-8 in WiShield\strings.c. And I removed the content type from the hostname.

It works!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜