Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Basic Authentication With URLConnection [closed]

connection = (HttpURLConnection)new URL(mURL).openConnection();
connection.setRequestMethod("GET");
String basicAuth = params[0] + ":" + params[1];
basicAuth = "Basic " + new String(Base64.encode(basicAuth.getBytes(),Base64.NO_WRAP));
connection.setRequestProperty("Authorization",basicAuth);
connection.connect();
InputStream in = connection.getInputStream();

I have gone through all possible questions based on this on SO but with out any success. I am trying to connect to my server using basic authentication. But when I try to access the input stream from URLConnection it throws IOException. Thanks for any help from any one of you.

like image 544
CodeDecode Avatar asked Mar 22 '26 10:03

CodeDecode


1 Answers

Basically, here there could be a few things that can go wrong. Does the URL you are trying to connect to exist? If not, may be you may have to redirect. Also, try

connection.getResponseCode();

and make sure it returns 200 or HTTP_OK

Needs more information to say anything. Go to debug mode and try to get the states of different objects used here.

Similar to IOException: "Received authentication challenge is null" (Apache Harmony/Android)

like image 66
BudsNanKis Avatar answered Mar 24 '26 00:03

BudsNanKis