2017-02-09 52 views
0

我建了一個請求:手動構建POST請求與CPP和提交PHP的形式與它

char* messege = "POST/HTTP/1.1\r\n" 
    "Host: musicshare.pe.hu \r\n" 
    "Content-Length: 18\r\n" 
    "Content-Type: text/plain\r\n" 
    "Accept: text/plain\r\n\r\n" 
    "command=michaeliko"; 

這一請求,我通過看一些拍攝wintin Wireshark的建立,因爲我沒有找到任何合適的指導它。

我得到一個確定的消息就可以了,但沒有「回聲」(打印)命令我做了PHP的一面。

因此,例如,常規的鉻捕捉響應看起來就像這樣:

Hello michaeliko\n 
    \n 
    \n 
    \n 
    <form method="post">\n 
    \n 
     Subject: <input type="text" name="command"> <br>\n 
     \n 
    </form>\t 

而對於POST請求我abouve表現,我會得到這個從網站服務器:

\n 
    \n 
    \n 
    <form method="post">\n 
    \n 
     Subject: <input type="text" name="command"> <br>\n 
     \n 
    </form>\t 

PHP方面看起來像這樣:

<?php 

if( isset($_POST['command'])) 
{ 
    $command = $_POST['command']; 
    echo 'Hello ' . $command . "\n"; 
} 

?> 



<form method="post"> 

    Subject: <input type="text" name="command"> <br> 

</form> 

我試圖通過很多方式來操縱這些代碼,但找不到答案。 我的要求有什麼問題?

回答

1

我還沒有評論,所以無法提出問題,例如:您如何提交數據?你想從PHP程序做到這一點?下面是我多年前寫的一個函數,我不知道它是不是你想要的;如果沒有,您可以嘗試cURL庫。

/* 
    * POST data to a URL with optional auth and custom headers 
    * $URL  = URL to POST to 
    * $DataStream = Associative array of data to POST 
    * $UP   = Optional username:password 
    * $Headers = Optional associative array of custom headers 
    */ 
    function hl_PostIt($URL, $DataStream, $UP='', $Headers = '') { 
    // Strip http:// from the URL if present 
    $URL = preg_replace('=^http://=', '', $URL); 

    // Separate into Host and URI 
    $Host = substr($URL, 0, strpos($URL, '/')); 
    $URI = strstr($URL, '/'); 

    // Form up the request body 
    $ReqBody = ''; 
    while (list($key, $val) = each($DataStream)) { 
     if ($ReqBody) $ReqBody.= '&'; 
     $ReqBody.= $key.'='.urlencode($val); 
    } 
    $ContentLength = strlen($ReqBody); 

    // Form auth header 
    if ($UP) $AuthHeader = 'Authorization: Basic '.base64_encode($UP)."\n"; 

    // Form other headers 
    if (is_array($Headers)) { 
     while (list($HeaderName, $HeaderVal) = each($Headers)) { 
     $OtherHeaders.= "$HeaderName: $HeaderVal\n"; 
     } 
    } 

    // Generate the request 
    $ReqHeader = 
     "POST $URI HTTP/1.0\n". 
     "Host: $Host\n". 
     "User-Agent: PostIt 2.0\n". 
     $AuthHeader. 
     $OtherHeaders. 
     "Content-Type: application/x-www-form-urlencoded\n". 
     "Content-Length: $ContentLength\n\n". 
     "$ReqBody\n"; 

    // Open the connection to the host 
    $socket = fsockopen($Host, 80, $errno, $errstr); 
    if (!$socket) { 
     $Result["errno"] = $errno; 
     $Result["errstr"] = $errstr; 
     return $Result; 
    } 

    // Send the request 
    fputs($socket, $ReqHeader); 

    // Receive the response 
    while (!feof($socket) && $line != "0\r\n") { 
     $line = fgets($socket, 1024); 
     $Result[] = $line; 
    } 

    $Return['Response'] = implode('', $Result); 

    list(,$StatusLine) = each($Result); 
    unset($Result[0]); 
    preg_match('=HTTP/... ([0-9]{3}) (.*)=', $StatusLine, $Matches); 

    $Return['Status'] = trim($Matches[0]); 
    $Return['StatCode'] = $Matches[1]; 
    $Return['StatMesg'] = trim($Matches[2]); 

    do { 
     list($IX, $line) = each($Result); 
     $line = trim($line); 

     unset($Result[$IX]); 

     if (strlen($line)) { 
     list($Header, $Value) = explode(': ', $line, 2); 

     if (isset($Return[$Header])) { 
      if (!is_array($Return[$Header])) { 
      $temp = $Return[$Header]; 
      $Return[$Header] = [$temp]; 
      } 

      $Return[$Header][] = $Value; 
     } 

     else $Return[$Header] = $Value; 
     } 
    } 
    while (strlen($line)); 

    $Return['Body'] = implode('', $Result); 

    return $Return; 
    } 
+0

不,你通過php發送請求,我通過C++來完成。我不需要用botton提交,因爲不是用bottun來確認表單,而是用C++代碼來完成。 –

+0

好的,我發佈時沒有看到該標籤。在這種情況下,您可以嘗試[libcurl](https://curl.haxx.se/libcurl/)或[boost](https://boostgsoc14.github.io/boost.http/) – alanlittle

+0

Yhea yhea man .. 。)我想知道事情是如何發展的,然後我會(也許......)進入更高的層次。 –