httpput authentication is not working

315 views Asked by At

I am using below code to authenticate on server.

DefaultHttpClient Client = new DefaultHttpClient();
Client.getCredentialsProvider().setCredentials(AuthScope.ANY,
          new UsernamePasswordCredentials("username", "password"));

HttpGet get = new HttpGet(Url);
HttpResponse response = Client.execute(put);

If I use get it will work fine for me.

but when I use HttpPut it's not working .

DefaultHttpClient Client = new DefaultHttpClient();
Client.getCredentialsProvider().setCredentials(AuthScope.ANY,
          new UsernamePasswordCredentials("username", "password"));

   HttpPut put = new HttpPut(Url);
   HttpResponse response = Client.execute(put);

I need to upload file on server, so need to use HttpPut for this.

This will through 401 (authentication) error.

Can anybody have idew, why did I face this issue?

Thanks, Embedded Team

1

There are 1 answers

0
Ioane Sharvadze On BEST ANSWER

You can authenticate through httpGET and than upload file using httpPUT, make sure you save cookies.

Why httpPut doesn't work? maybe its server side requirement to authenticate using GET. But surely it's a bad idea to use GET request for auth.