簡體   English   中英

Jakarta Commons HTTPClient無法使用php在我的Apache服務器上發送發布請求

[英]Jakarta commons HTTPClient can't send a post request on my apache server with php

我已經建立了一個Apache服務器,並運行一個php腳本來處理各種發布請求。 該腳本如下所示:

$uploaddir = realpath('mypath');
//realpath('./') . '/';
$uploadfile = $uploaddir . '/' . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";

而且效果很好,它可以處理請求並將文件移到我想要的位置。

但是當我嘗試使用以下Java客戶端進行此操作時,我什么也沒得到:

    String URL = "http://localhost/accept.php";
    File file = new File("C:/Ozer/ANPRProject/MDT/Export/test.txt");

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URL);

    try{
        System.out.println(""+file.exists());

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file),-1);

        reqEntity.setContentType("application/octet-stream");
        reqEntity.setChunked(true);

        httppost.setEntity(reqEntity);
        System.out.println("execute request " + httppost.getRequestLine());

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        if (resEntity != null) {
            System.out.println("Response content length: " + resEntity.getContentLength());
            System.out.println("Chunked: " + resEntity.isChunked());
        }
        EntityUtils.consume(resEntity);

    } catch(Exception e) {

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

我得到的系統輸出是:

 true
 execute request POST http://localhost/accept.php HTTP/1.1
 ----------------------------------------
 HTTP/1.1 200 OK
 Response content length: 330
 Chunked: false

我覺得很奇怪,因為未將分塊設置為true? 雖然我在代碼中特別要求它。 但是無論如何,上傳是行不通的。 我已將contentType設置為“ application / octet-stream”,因為我曾經請求發布的php腳本給了我反饋:[type] => application / octet-stream

因此,apache的日志告訴我連接成功,並且他確實收到了請求:

 127.0.0.1 - - [08/Aug/2013:16:10:04 +0200] "POST /accept.php HTTP/1.1" 200 330 "-" ""Apache-HttpClient/4.2.5 (java 1.5)"

這是我使用Php腳本請求帖子時得到的:

 127.0.0.1 - - [08/Aug/2013:15:54:23 +0200] "POST /accept.php HTTP/1.1" 200 419 "-" "-" ::1 - - [08/Aug/2013:15:54:23 +0200] "GET /testSendDat.php HTTP/1.1" 200 3330 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"

和來自PHP腳本的錯誤日志顯示給我:

 [08-Aug-2013 16:10:04 Europe/Berlin] PHP Notice:  Undefined index: file_contents

所以我知道這意味着什么,我要從中讀取文件的數組為空。 但為什么? 我沒有任何線索。 我猜我使用的是錯誤的方式來發布請求,但是現在已經尋找了一段時間,而實際上並沒有成功地找到問題所在。 幫助將非常有必要。

哦,設置apache tomcat服務器並使用httpservlets可能會更容易些嗎? 還是那不重要嗎?

您不會創建一個HTTP-Request來模擬其中帶有文件上載元素的已填寫表單。 您使用文件作為請求正文創建HTTP POST請求。

填寫的表單POST HTTP Request的正文如下所示:

user=Fritz&age=12

它看起來像GET HTTP請求的URL參數。

帶有文件上傳的已填寫表單POST是包含多個正文的多部分HTTP請求。 其中一個具有表單字段,另一個具有上載文件內容。

請參閱: http文件上傳工作方式

我無法提供樣本來設置您的請求,但是此信息可能會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM