java android - download file http authentication -
i want download content of html , json files webserver http authentication on android. on browsers used http://username:passwort@example.com/path/to/something, worked fine. in java on android doesn't work (it worked fine before adding http authoriziation). can show html file in webview using this. how download content? filenotfoundexception. code download html file:
string url = "http://username:passwort@example.com/path/to/something"; //or: "http://example.com/path/to/something" url oracle = new url(url); urlconnection yc = oracle.openconnection(); //error thrown following line bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(yc.getinputstream())); string inputline; stringbuilder stringbuilder = new stringbuilder(); while ((inputline = bufferedreader.readline()) != null) { stringbuilder.append(inputline + "\n"); } return stringbuilder.tostring(); another way tried download json file:
url url = new url(urlstring); string encoding = base64.encode("username:password".getbytes(), base64.default).tostring(); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("post"); connection.setdooutput(true); connection.setrequestproperty("authorization", "basic " + encoding); //error thrown in following line = (inputstream) connection.getinputstream(); or using defaulthttpclient instead of httpurlconnection
url url = new url(urlstring); string encoding = base64.encode("username:password".getbytes(), base64.default).tostring(); defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(urlstring); httppost.setheader("authorization", "basic " + encoding); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); in end want read content:
bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); string json = sb.tostring(); string json using defaulthttpclient "401 authorization required..." added authorization header, didn't i? , it's empty using httpurlconnection.
is http problem? java problem? android problem??? me please!!
Comments
Post a Comment