2017-10-09 3 views
0

この関数はwxWidgetsを使用するPHP-CLIスクリプトの一部で、LAN上のApache2 Webサーバーにいくつかのフィールドをポストしようとします。 "400 Bad Request"に失敗し、 "/blog/update.php"コードは入力されません。 このエラーの重大な部分である可能性のあるドメイン/ホストの値は正しいです。 なぜそれは悪い要求ですか? 私のようなすべての質問には答えがありません。「400 Bad Request」なぜですか?

<?php 

[...] 

function upload_button_clicked() 
{ global $file_box, $frame, $html_box, $remote_host, $title_box, $upload_button ; 
    // collect global variables 
    $upload_button->Disable(); 
    $file = $file_box->GetValue(); 
    $title = $title_box->GetValue(); 
    $html = $html_box->GetValue(); 

    // open stream to server 
    $messages = ""; 
    $server = fsockopen($remote_host, 80, $errno, $errstring, 5); 
    if(!$server) 
    { $messages .= "Cannot connect to $remote_host: $errno $errstring\n"; 
     goto end; 
    } 
    $messages .= "Connected to $remote_host\n"; 
    stream_set_timeout($server, 5); 

    // urlencode parameters 
    $body = "date=" . urlencode(date('d m Y')) 
       ."&file=" . urlencode($file) 
       ."&title=" . urlencode($title) 
       ."&html=" . urlencode($html) ; 
    $body_length = strlen($body) +1; for the "\n" to terminate 

    // build POST request 
    $request = "POST /blog/update.php HTTP/1.1\n" 
       ."Host: $remote_host\n" 
       ."Content-Type: application/x-www-form-urlencoded\n" 
       ."Content-Length: $body_length\n" 
       ."\n" 
       .$body."\n"; 


    // fwrite request to stream 
    loop: 
    $request_length = strlen($request); 
    $bytes_written = fwrite($server, $request, $request_length); 
    if($bytes_written === false) 
    { $messages .= "Writing POST to $remote_host failed"; 
     goto end ; 
    } 

    // deal with partial write 
    if($bytes_written < $request_length) // not all written 
    { $request = substr($request, $bytes_written); 
     goto loop; 
    } 
    $messages .= "sent $file OK\n"; 

    // read responses to POST 
    while(!feof($server)) 
    { $response = fgets($server, 1024); 
     $messages .= "$response\n"; 
    } 

    // tidy up 
    end: 
    $report = new wxMessageDialog($frame, $messages, "Upload", wxOK|wxCENTRE); 
    fclose($server); 
    $upload_button->Enable(); 
    return ; 
} 

[...] 

?> 

ターミナル出力:。

Connected to mydomain.com 
sent /home/.../blog/contents/201710090746+title.htm OK 
HTTP/1.1 400 Bad Request 

Date: Sun, 08 Oct 2017 21:46:08 GMT 

Server: Apache 

Content-Length: 285 

Connection: close 

Content-Type: text/html; charset=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 Server at 127.0.0.1 Port 80</address> 

</body></html> 

答えて

0

あなたが$bodyの最後に"\n"を追加しているので、あなたは、Apacheに送るの体は少し長いstrlen($body)より

$body_length = strlen($body."\n"); 

をお試しくださいこれにより少なくともone other coderに400 Bad Requestが発生しました。だけではなく、\nこのような

+0

感謝。私は上記のコードで "+1"を追加しましたが、修正しませんでした。 – Palloy

0

てみ使用\r\n:それをスポッティングため

$body_length = strlen($body); 

// build POST request 
$request = "POST /blog/update.php HTTP/1.1\r\n" 
      ."Host: $remote_host\r\n" 
      ."Content-Type: application/x-www-form-urlencoded\r\n" 
      ."Content-Length: $body_length\r\n" 
      ."\r\n" 
      .$body; 
+0

それはうまくいった!なぜサーバーがUbuntu 17.04で動作しているApacheで、クライアントがUbuntu 16.04になっているのですか? – Palloy

+0

標準的なので@Palloy https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html – Kazz

+0

あなたは毎日何かを、おかげで学びます。 – Palloy

関連する問題