Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify HTTP request headers from an Android app?

Tags:

android

http

Is there a way for me to capture all HTTP requests that are being sent from my app, and modify their headers before they are sent? I want to modify their referer header so that the server that the requests are sent to thinks as if they are coming from a web browser instead of a mobile application. Thank you!

Update: To give you more context, I am planning to port a chrome extension Instant Music into an Android app by using Phonegap. Some YouTube videos that are allowed on PC are not allowed on mobile and I suspect that's because youtube player embedded inside an android app doesn't have a referrer header. I am trying to find a solution to this problem so that I can play such videos on mobile too.

like image 808
Maximus S Avatar asked Jun 23 '14 12:06

Maximus S


People also ask

Can HTTP headers be modified?

You cannot modify or remove HTTP request headers whose name starts with x-cf- or cf- except for the cf-connecting-ip HTTP request header, which you can remove. You cannot modify the value of any header commonly used to identify the website visitor's IP address, such as x-forwarded-for , true-client-ip , or x-real-ip .

How do I add custom HTTP request headers?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.


1 Answers

Youtube detects user browser's Agent String which contains information about the browser. If you were to use a WebView to show the youtube video instead, then it would be possible to set that WebView's Agent String. You can find agent strings of different browsers on the internet. I found some here: Agent Strings.

Here's how i play Bob Marley's song that is not allowed on mobile phones by impersonating a Firefox browser:

package com.my.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

    public class MyActivity extends Activity {

        private WebView mWebView ;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mWebView  = new WebView(this);
            // Enable javascript
            mWebView.getSettings().setJavaScriptEnabled(true);
            // Impersonate Mozzila browser
            mWebView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:21.0.0) Gecko/20121011 Firefox/21.0.0");
            final Activity activity = this;

            mWebView.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                }
            });

            mWebView .loadUrl("http://youtube.com/watch?v=x59kS2AOrGM");
            setContentView(mWebView);
        }

    }

EDIT:

You also need to give permission for your Activity to use the internet, by adding this line to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> 
like image 173
Simas Avatar answered Oct 22 '22 17:10

Simas