我正在使用 Ethernet shield的 Arduino 设备上发出发布请求。该服务器是一个Node.js服务器,我在服务器端处理了 post 请求,结果与福尔马林和强大的结果相同。POST 已处理,但未完成文件传输。
加载开始:{“时间”:1339574854222}“字节接收”:178,“文件完成”:0
这是发送帖子的代码:
String boundary = "--73249889599006000";
String URL = "/upload";
String contentType = "text/plain";
String fileName = "text.txt";
void sendData(){
String thisFile = "This is not the contents of thisFile";
Serial.println("connecting...");
// If you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
String postHeader = "POST " + URL + " HTTP/1.1\n";
postHeader += "Content-Type: multipart/form-data; boundary=";
postHeader += boundary + "\n";
postHeader += "Host: 192.168.3.78\n";
postHeader += "User-Agent: Arduino\n";
postHeader += "Referer: http://192.168.3.78/upload\n";
String requestHead = "\n--" + boundary + "\n";
requestHead += "Content-Disposition: form-data; name=\"upload\"; filename=\"" + fileName + "\"\n";
requestHead += "Content-Type: " + contentType + "\n\n";
String tail = "\n--" + boundary + "--\n\n";
int contentLength = requestHead.length() + thisFile.length() + tail.length();
postHeader += "Content-Length: " + String(contentLength, DEC) + "\n\n";
char charBuf0[postHeader.length() + 1];
postHeader.toCharArray(charBuf0, postHeader.length() + 1);
client.write(charBuf0);
char charBuf1[requestHead.length() + 1];
postHeader.toCharArray(charBuf1, requestHead.length() + 1);
client.write(charBuf1);
char charBuf2[thisFile.length() + 1];
postHeader.toCharArray(charBuf2, thisFile.length() + 1);
client.write(charBuf2);
char charBuf3[tail.length() + 1];
postHeader.toCharArray(charBuf3, tail.length() + 1);
client.write(charBuf3);
}