How to access authenticated URL in Android?

I want to analyze data from a password protected RSS feed. If I try to parse the data, it throws a "File not found Exception"

Here is my code:

try {
                DBF = DocumentBuilderFactory.newInstance();
                DB = DBF.newDocumentBuilder();
                url_val = new URL("http://admin:admin@dev.quaddeals.com/university-of-illinois/androids/city.rss");
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                try {
                    dom = DB.parse(url_val.openConnection().getInputStream());
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                elt = dom.getDocumentElement();
                NodeList items = elt.getElementsByTagName("item");
                for (int i = 0; i < items.getLength(); i++) {
                    Node item = items.item(i);
                    NodeList properties = item.getChildNodes();
                    for (int j = 0; j < properties.getLength(); j++) {
                        Node property = properties.item(j);
                        String name = property.getNodeName();

                        if (name.equalsIgnoreCase("title")) {
                            cityTitle = property.getFirstChild().getNodeValue();                                
                        }
                        if (name.equalsIgnoreCase("id")) {

                            cityId = property.getFirstChild().getNodeValue();
                        }
                    }
                    title.add(cityTitle);
                    id.add(cityId);
                }
                   VALUE1 = new String[title.size()];
                    Iterator<String> itc = title.iterator();
                    while (itc.hasNext()) {
                        l1++;
                        VALUE1[l1] = itc.next().toString();                 
                    }

                    VALUE2 = new String[id.size()];
                    Iterator<String> it1c = id.iterator();
                    while (it1c.hasNext()) {
                        m1++;
                        VALUE2[m1] = it1c.next().toString();
                    }

        } catch (Exception e) {



        }

My Cat log shows the error as follows:

02-21 23:20:48.893: WARN/System.err(19206): java.io.FileNotFoundException: http://dev.quaddeals.com/university-of-illinois/androids/city.rss

02-21 23:20:48.893: WARN/System.err(19206):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)

02-21 23:20:48.893: WARN/System.err(19206):     at com.fsp.quaddeals.DealCities$SelectDataTask_Deals.doInBackground(DealCities.java:341)

02-21 23:20:48.893: WARN/System.err(19206):     at com.fsp.quaddeals.DealCities$SelectDataTask_Deals.doInBackground(DealCities.java:1)

If I enter this URL in the browser, it will ask for authentication, after which it will be redirected to the original RSS FEED, where my parsing data exists.

I want to analyze the data on this page. How can I access this password protected page and analyze the data? Does Android support SSL connections?

+3
source share
1 answer

. , :

URL url = new URL("http://admin:admin@dev.quaddeals.com/university-of-illinois/androids/city.rss");
URLConnection urlConnection = url.openConnection();

if (url.getUserInfo() != null) {
    String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
    urlConnection.setRequestProperty("Authorization", basicAuth);
}

dom = DB.parse(urlConnection.getInputStream());

java.net.Authenticator, ( URL-):

// see https://developer.android.com/reference/java/net/Authenticator.html
// and https://developer.android.com/reference/java/net/PasswordAuthentication.html
Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("admin", "admin".toCharArray());
    }
});

, Volley () , okhttp (example).

+1

Source: https://habr.com/ru/post/1793659/


All Articles