Android - Default user agent for URLConnection?
Asked Answered
E

3

11

I am creating a regular HTTP connection using this code:

URLConnection cn = new URL( "http://...." ).openConnection();
cn.connect();

How do I find out the default user agent for my HTTP connection? I tried using the following codes but they all return null:

Log.d("My app", "User agent = " + cn.getRequestProperties().get("User-Agent"));
Log.d("My app", "User agent = " + cn.getHeaderField("User-Agent"));
Ecumenical answered 5/1, 2013 at 4:31 Comment(0)
E
23

The default user agent is null because the header is empty by default. You will have to set it manually using:

cn.setRequestProperty("User-Agent","your user agent");
Epicure answered 5/1, 2013 at 4:47 Comment(0)
T
6

With default user agent:

URLConnection cn = new URL("http://....").openConnection();
cn.setRequestProperty("User-agent", System.getProperty("http.agent"));
cn.connect();
Thaliathalidomide answered 9/8, 2015 at 12:50 Comment(0)
A
0

which likes this:

User-Agent: Dalvik/2.1.0 (Linux; U; Android 14; sdk_gphone64_arm64 
Assentor answered 24/8 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.