2015-10-19 61 views
-1

我想從我的android應用程序傳輸文件到http地址。我寫下面的代碼,但沒有工作。地址hhtp=192.168.0.1:8181username=adminpassword=blankAndroid的http文件傳輸

我應該在哪裏進行HTTP文件傳輸

String url = "http://192.168.0.1:8181"; 
File dir = Environment.getExternalStorageDirectory(); 

File file = new File(dir,DATABASE_NAME); 
try { 
    HttpClient httpclient = new DefaultHttpClient(); 

    HttpPost httppost = new HttpPost(url); 

    InputStreamEntity reqEntity = new InputStreamEntity(
      new FileInputStream(file), -1); 
    reqEntity.setContentType("binary/octet-stream"); 
    reqEntity.setChunked(true); // Send in multiple parts if needed 
    httppost.setEntity(reqEntity); 
    HttpResponse response = httpclient.execute(httppost); 
    //Do something with response... 
    Log.e("file transfer", "done"); 
} catch (Exception e) { 
    // show error 
    Log.e("error", e.getMessage()); 
} 
+0

哪種身份驗證服務器是否會要求?對於例如基本身份驗證,您可以添加一個'Authorization'頭。 – Henry

+0

其實它的dlink dir506設備地址。當我在瀏覽器頁面的adrress欄中輸入地址192.168.0.1:8181時,它會要求輸入用戶名和密碼。所以我不知道授權類型 –

回答

0

設置的用戶名和密碼,根據使用最有可能的基本身份驗證的意見。有關詳細信息,請參見RFC 1945,第11.1節。這意味着一個表頭

Authorization: Basic <base64-encoded-auth-info> 

必須添加到請求。 base64-encoded-auth-info是字符串username:密碼:

admin:blank 

使用base64編碼。

一起:

Authorization: Basic YWRtaW46Ymxhbms= 
+0

您可以發佈任何示例的鏈接作爲im新的它 –

+0

我不是很熟悉Apache HTTP客戶端和順便說一句。它現在在Android中也被棄用;但這個鏈接可能會有所幫助:http://stackoverflow.com/questions/2014700/preemptive-basic-authentication-with-apache-httpclient-4/4328694#4328694 – Henry

+0

它不工作任何鏈接 –