Android http文件传输

时间:2015-10-19 11:18:49

标签: android androidhttpclient

我想将文件从我的Android应用程序传输到http地址。我编写了以下代码但不起作用。hhtp=192.168.0.1:8181username=adminpassword=blank的地址。

我应该在哪里设置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());
}

1 个答案:

答案 0 :(得分:0)

根据评论,最有可能使用基本身份验证。有关详细信息,请参阅RFC 1945,第11.1节。这意味着表单的标题

base64-encoded-auth-info
必须将

添加到请求中。 admin:blank 是字符串username:password:

Authorization: Basic YWRtaW46Ymxhbms=

用base64编码。

所有在一起:

'1' matches (as e.g. 12/12/1999 is a valid date)
'4' does not match
'04' matches
'12/12/' matches