Determine if HTTP requests are being requested from an Android application or not? and then respond accordingly

My Android app has an app widget associated with it that updates every 10 minutes on an Android device. These updates send HTTP requests for data to the servers and analyze the server’s response and update the application as needed.

At the moment, if you send this URL from browsers on your laptop or PC, the server will respond and update everything that is required in the database on the server.

What I want to do is when HTTP requests are received on the server, I want to determine if the request came from my Android application from an Android device, and then responded with data. I would like to change the PHP code on the server so that they display or redirect to some page if the HTTP request came from a browser or everything else except my Android application.

Typical HTTP requests from applications are similar to http://example.com/abc.php?usera=abc&datab=xyz

I do not want to respond to this URL in the same way if it comes from anywhere other than an Android application. Is it possible? Which would be a good way to achieve this.

Thank you for your help.

+3
source share
4 answers

, .

, MD5, ( ). , .

, , .

http://example.com/abc.php?usera=abc&datab=xyz×tamp=123456789, timestamp - ( unix) :

public static String makeCheck(String url)
{
    URL u=new URL(url);
    MessageDigest md = MessageDigest.getInstance("MD5");
    u.getQuery();
    md.update(u.getQuery().getBytes());
    BigInteger bn = new BigInteger(1,md.digest("A_SECRET_WORD".getBytes()));
    return bn.toString(16);
}

, - :

request.addHeader("X-CHECKSUM", makeCheck(url) );

:

if (md5($_SERVER['QUERY_STRING']."A_SECRET_WORD")!=$_SERVER['X-CHECKSUM']) {
    // Wrong checksum
}

$timediff=60;

if ( $_GET['timestamp']>(time()+$timediff) || $_GET['timestamp']<(time()-$timediff) ) {
    // Bad timestamp
}

, , .

+7

User-Agent HTTP-. , . , Nexus One, Froyo, User-Agent:

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

, HttpClient , User-Agent, HttpClient, : Android HTTP User Agent.

User-Agent, , Android .

+3

HttpClient android,

 client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "MY Android device identifier");

USER_AGENT HTTP, . USER_AGENT, , Android.

+1

(, POST GET, ), , .

( . @mark_bakker nd @mark_allison), , , , , .

  • Android user_agent , , , / . "Android- , / "
  • , , Android, , , , , , .

In the end, it might be better to just change your request if you can: you want a different answer, you have to make another request, this is my opinion.

+1
source

All Articles