I am using HttpUrlConnection
to make network requests from my android application. Everything works fine except one thing, 401. Whenever the server returns response
try to use HttpClient
private void setCredentials(String login, String password) {
if (login != null && password != null)
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(URL_HOST, 80), new UsernamePasswordCredentials(login, password));
}
private InputStream execute(String url, String login, String password) {
setCredentials(login, password);
HttpGet get = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(get);
int code = response.getStatusLine().getStatusCode();
if (code == HttpURLConnection.HTTP_OK) {
InputStream stream = response.getEntity().getContent();
return stream;
} else {
Log.e("TAG", "Wrong response code " + code + " for request " + get.getRequestLine().toString());
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}