2017-10-09 80 views
0

該函數是使用wxWidgets的PHP-CLI腳本的一部分,該腳本會嘗試將一些字段發佈到LAN上的Apache2 Web服務器。 它總是失敗「400錯誤請求」,並且不會輸入「/blog/update.php」代碼。 域/主機的值可能是此錯誤的關鍵部分,是正確的。 爲什麼這是一個不好的要求? 所有和我一樣的問題都沒有答案。「400錯誤請求」爲什麼?

<?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_length = strlen($body."\n"); 

發送到Apache的身體比strlen($body)稍長一點,因爲你是一個附加到"\n"$body結束。至少one other coder因此得到了400個錯誤的請求。

+0

感謝察覺這一點。我在上面的代碼中添加了「+1」,但沒有修復它。 – Palloy

0

嘗試使用\r\n而不是僅僅\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

@Ployloy,因爲它在標準的https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html – Kazz

+0

你每天都會學到一些東西,謝謝。 – Palloy