【发布时间】:2018-03-12 21:17:39
【问题描述】:
我尝试对我的 php 服务器进行 HTTP POST,但我在服务器日志中收到错误请求:
:192.168.1.15 - - [12/Mar/2018:17:46:16 -0300] "POST \xa9\x020\x91\xa7\x020\x93\xa9\x02 \x93\xa7\x02.\xea2\xe0\xa2\xeb\xb2\xe0\xf9\x01B\x91\x84\x0f\x91\x1dN\x91\x8c\x93f#\x11\xf0@\x83\x01\xc0\x10\x82\x89/\x99'D\xe0N\x0fB\x13\xf0\xcf\x10\x92\xb6\x02\x10\x92\xb7\x02\x80\xe5\x80\x93\xb2\x02\b\x95\x0f\x93\x1f\x93\xcf\x93\xdf\x93p\xe0lW}O!0!\xf4\x8a\x01\x07_\x1fO\x0c\xc0\"09\xf0 HTTP/1.0" 400 471 "-" "-"
如果尝试使用 HTTP GET,则会成功并在服务器日志中获得预期的请求:
192.168.1.15 - - [12/Mar/2018:11:26:23 -0300] "GET /SafeHome/index.php HTTP/1.0" 200 8 "-" "-"
我正在使用:
Arduino Uno;
Ethernet Modulo ENC28J60;
Library: <EtherCard.h>;
Class: ether.httpPost;
我的 httpPost 请求有什么问题?
为什么日志请求有这种奇怪的格式“POST \xa9\x020\x91\xa7\x020\x93\xa9\x02...”?
以下完整代码:
#include <EtherCard.h>
#define REQUEST_RATE 5000 // milliseconds
// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x33 };
static byte myip[] = { 192,168,1,15 };
static byte gwip[] = { 192,168,1,1 };
static byte netmask[] = { 255,255,255,255 };//
static byte dnsip[] = { 8,8,8,8 };
static byte hisip[] = { 192,168,1,1 };//
const char website[] PROGMEM = "192.168.1.1";
byte Ethernet::buffer[300];
static long timer;
void setup () {
Serial.begin(9600);
Serial.println("\n[getStaticIP]");
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip, gwip);
if(ether.dnsLookup (website, false)){
Serial.println( "dnsLookup ok");
}else{
Serial.println( "dnsLookup faild");
ether.parseIp(ether.hisip, "192.168.1.1"); //IP do servidor manual
}
while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("Gateway found");
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
ether.printIp("SRV: ", ether.hisip);
timer = - REQUEST_RATE; // start timing out right away
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
ether.hisport = 90;//to access local host
char *PATH="/SafeHome/addevent";
char *contentType = "Content-Type: application/json";
String postDataStr = "{\"INSTALACAO_ID\": \"1\", \"TPEVENTO_ID\": \"1\",\"ZONA_ID\": \"1\"}";
char *postDataChar = postDataStr.c_str();
ether.httpPost(PATH, website, contentType, postDataChar, my_result_cb);
}
}
// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
- 注意:服务端的所有服务都可以正常工作,可以通过 PostMan 使用 POST 和 GET。
GET 请求的 Arduino 代码:
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
ether.hisport = 90;//to access local host
ether.browseUrl(PSTR("/SafeHome/index.php"),"",website, my_result_cb);
}
}
为 GET 请求打印的此服务器日志:
192.168.1.15 - - [12/Mar/2018:11:20:55 -0300] "GET /SafeHome/index.php HTTP/1.0" 200 8 "-" "-"
【问题讨论】:
-
那个 stranger format 是你的字符串的正确十六进制格式,因为我无法测试为什么现在在 Arduino 上会发生这种情况,你能发布工作代码吗GET 请求,以便我们查看差异?
-
嗨,GrowingBrick,我用 GET 请求的代码和服务器日志编辑了我的帖子。
标签: c++ json http post arduino-uno