-1

I want to transfer file from my android application to http address.I written following code but doesnt work.The address of hhtp=192.168.0.1:8181 with username=admin and password=blank.

Where should i set username and password for http file transfer

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());
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Lalit Jadiya
  • 213
  • 4
  • 14
  • Which kind of authentication does the server require? For Basic authentication for instance you would add an `Authorization` header. – Henry Oct 19 '15 at 11:26
  • Actually its dlink dir506 device address.When i enter the address 192.168.0.1:8181 in adrress bar of browser page it then ask for username and password .So i dont know type of Authorization – Lalit Jadiya Oct 19 '15 at 11:30

1 Answers1

0

According to the comments most likely Basic authentication is used. See RFC 1945, section 11.1 for the details. This means a header of the form

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

must be added to the request. The base64-encoded-auth-info is the string username:password:

admin:blank

encoded with base64.

All together:

Authorization: Basic YWRtaW46Ymxhbms=
Henry
  • 42,982
  • 7
  • 68
  • 84
  • Can you post any link of the example as i m new to it – Lalit Jadiya Oct 19 '15 at 12:13
  • I am not really familiar with the Apache HTTP client and btw. it is also deprecated in Android now; but this link may help: http://stackoverflow.com/questions/2014700/preemptive-basic-authentication-with-apache-httpclient-4/4328694#4328694 – Henry Oct 19 '15 at 12:18