How to send an HTTP request using an http header

Thanks in advance...

im using this code to set the http header in the http request for url authentication ..

but I think some of them interfere, so I could not get an answer ...

the answer still comes as "authorization required" ...

httpParameters = new BasicHttpParams(); String auth = android.util.Base64.encodeToString( ("florin999@zitec.ro" + ":" + "test2323" + ":" + "zitec" + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7") .getBytes("UTF-8"), android.util.Base64.NO_WRAP); HttpConnectionParams.setSoTimeout(httpParameters, 0); client = new DefaultHttpClient(httpParameters); String getURL = "URL here"; get = new HttpGet(getURL); get.addHeader("Authorization", "Basic "+ auth); // get.addHeader("X-ZFWS-Accept", "text/json"); HttpResponse responseGet = client.execute(get); HttpEntity resEntityGet = responseGet.getEntity(); if (resEntityGet != null) { //do something with the response //Toast.makeText(Login.this.getApplicationContext(),EntityUtils.toString(resEntityGet), Toast.LENGTH_SHORT).show(); String s = EntityUtils.toString(resEntityGet); tv1.setText(s); } }catch(Exception e){ } 

plssss help me as soon as possible

+8
android
source share
1 answer

Yor code is ok. you have the http client setup configured and the header added correctly. but. your basic authentication is incorrect. the header key (Authentication) is right, but the value must be Basic + Base64.encode (username + ":" + pass)

also an alternative to this is the following code:

  httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password")); 

The target hostname is probably null and port is -1.

but this one that you cannot use if you use a URL connection, so the header is also acceptable.

EDIT:

HERE IS YOUR QUESTION:

 String auth = android.util.Base64.encodeToString( ("florin999@zitec.ro" + ":" + "test2323" + ":" + "zitec" + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7") .getBytes("UTF-8"), android.util.Base64.NO_WRAP); 

What are all these concatenations?

Basic authentication means adding an authentication header with base64 encoding, which is the word of the word "Basic " + Base64.encodeToString("yourUsername"+":"+"yourPassword)

An alternative is to add the method i inserted at the top of the credential provider in the same way

+8
source

All Articles