Search code examples
androidhttphttpsgoogle-dfpadview

Force HTTP protocol to HTTPS on response to AdRequest


How and where can I change the HTTP protocol of the response I get for my (Publisher)Adview from HTTP to HTTPS? This to enable my adview to show ads from third party networks which are now being blocked. I have had contact with the Google Mobile Ads SDK support team and their answer was that adding an S to the HTTP protocol of the response of the ad request will make it possible to show the ads. But I can't find where and how I can do this.

Below is the error I receive in the logcat:

10-23 19:56:15.813 18492-18492/com.koeck.verdienapp W/chromium: [WARNING:web_contents_impl.cc(2990)] https://pubads.g.doubleclick.net ran insecure content from http://ib.adnxs.com/ttj?id=4433225&size=300x250&referrer=com.koeck.android&cb=774743058&psa=false&position=above 10-23 19:56:15.823 18492-18492/com.koeck.verdienapp W/chromium: [WARNING:web_contents_impl.cc(2990)] https://.. ran insecure content from http://.. 10-23 19:56:15.823 18492-18492/com.koeck.verdienapp W/Ads: JS: Mixed Content: The page at 'https://..' was loaded over HTTPS, but requested an insecure script

At this point I have come up with this code so far to change HTTP to HTTPS:

public void changeHTTP(URL url) throws IOException {
            if ("http".equals(url.getProtocol())) {
                String urlSource = "";

            try {
                urlSource = URLDecoder.decode(url, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace(); // This should not happen.
            }

            Log.d(TAG, "decoded: " + urlSource);

            String urlNew = urlSource.replace("http","https");
            }
    }

Thanks for taking the time to read my question.

Kind regards, Rogier van den Brink


Solution

  • It's even easier than you think:

    public URL changeHTTP(URL url) throws IOException {
         String strUrl = url.toString();
         if (strUrl.indexOf("http://", 0) == 0)
         {
             strUrl = strUrl.replace("http://", "https://");
             url = new URL(strUrl);
         }
         return url;
    }