2

イーサネットシールドを備えた Arduino デバイスでポスト リクエストを作成しています。サーバーはNode.jsサーバーであり、ホルマリンと手ごわいの両方で同じ結果でサーバー側のポストリクエストを処理しました。POST は処理されますが、ファイル転送は完了しません。

loadstart: {"time":1339574854222} "bytesReceived": 178、"filesCompleted": 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);
    }
4

1 に答える 1

1

行末と他のいくつかの調整により、それがソートされました。これは、ホルマリンでうまく機能する更新されたコードです。

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\r\n\r\n";

  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\r\n";
    postHeader += "Content-Type: multipart/form-data; boundary=";
    postHeader += boundary + "\r\n";
    postHeader += "Host: 192.168.3.18\r\n";
    postHeader += "User-Agent: Arduino\r\n";
    postHeader += "Referer: http://192.168.3.18/upload\r\n";

    String requestHead = "--" + boundary + "\r\n";
    requestHead += "Content-Disposition: form-data; name=\"upload\"; filename=\"" + fileName + "\"\r\n";
    requestHead += "Content-Type: " + contentType + "\r\n\r\n";

    String tail = "--" + boundary + "--\r\n\r\n";

    int contentLength = requestHead.length() + thisFile.length() + tail.length();

    postHeader += "Content-Length: " + String(contentLength, DEC) + "\n\n";
    Serial.print("Content-Length: ");
    Serial.println(String(contentLength, DEC));

    char charBuf0[postHeader.length() + 1];
    postHeader.toCharArray(charBuf0, postHeader.length() + 1);
    client.write(charBuf0);
    Serial.print(charBuf0);

    char charBuf1[requestHead.length() + 1];
    requestHead.toCharArray(charBuf1, requestHead.length() + 1);
    client.write(charBuf1);

    char charBuf2[thisFile.length() + 1];
    thisFile.toCharArray(charBuf2, thisFile.length() + 1);
    client.write(charBuf2);
    Serial.print(charBuf2);

    char charBuf3[tail.length() + 1];
    tail.toCharArray(charBuf3, tail.length() + 1);
    client.write(charBuf3);
    Serial.print(charBuf3);
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
于 2012-06-15T13:21:27.453 に答える