我从我的arduino设备发送邮件请求到我的托管服务器它给我400错误请求错误。 相同的代码在我的localhost中运行良好,但不在托管服务器上运行。 我的arduino代码如果在下面给出。
void callApi(String ID,String path,int comm,String message,int isResponseNeeded) {
String serverPath="www.xyz.com";
String data = "{" ;
data = data + "\"id\": \""+ID+"\"," ;
data = data + "\"version\": \""+VERSION+"\"," ;
data = data + "\"command\": \""+comm+"\"," ;
data = data + "\"message\": \""+message+"\"" ;
data = data + "}" ;
Serial.print("Send data...on=>");
Serial.println(path);
if (wifiClient.connect(serverPath,80)) {
Serial.println(data);
wifiClient.println("POST /abc/xyz HTTP/1.1");
wifiClient.println("Content-Type: application/json");
wifiClient.print("Host: ");
wifiClient.println(serverPath);
wifiClient.print("Content-Length: ");
wifiClient.println(data.length());
wifiClient.print("\n");
wifiClient.print(data);
wifiClient.print("\n");
Serial.println("Data sent...Reading response..");
if(isResponseNeeded>0){
unsigned long timeout = millis();
while (wifiClient.available() == 0) {
if (millis() - timeout > 10000) {
Serial.println(">>> Client Timeout !");
break;
}
}
String response;
while(wifiClient.available()){
String line = wifiClient.readStringUntil('\n');
response=line;
Serial.println(response);
}
Serial.println(response.length());
}
{" id":" _60_1_94_f_a9_3c _","版本":" 1.10","命令": " 0","消息":""}
发送的数据......阅读回复..
HTTP / 1.1 400错误请求
日期:星期六,2017年5月20日05:13:41 GMT
服务器:Apache / 2.4.18(Ubuntu)
内容长度:324
连接:关闭
Content-Type:text / html;字符集= ISO-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at ubuntu-512mb-nyc2-01.localdomain Port 80</address>
</body></html>
&#13;
=============================================== ============ 还有一件我观察过的事情。它运行GET请求很好。只有在托管服务器上发出POST请求时才会出现问题。
答案 0 :(得分:0)
我想API服务要求POST内容和正文为application / x-www-form-urlencoded编码。