×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    Unhappy how to get browser user agent

    hi,
    求助!
    我想获得手机自带浏览器访问网络的所使用的user agent,在我的程序访问网络的时候,将信息添加在我的user agent中去,但是在网上只有二版的方法,没有三版的方法,求大侠帮助一下。

    另外,我参照过网上的一个方法,代码如下:
    CMyTelephony* telephony = CMyTelephony::NewL();
    telephony->GetPhoneId();
    aPhoneType.Copy(telephony->iPhoneId.iManufacturer);
    aPhoneType.Append(_L(" "));
    aPhoneType.Append(telephony->iPhoneId.iModel);
    delete telephony;
    但只能得到手机型号,比如: Nokia E63 ,Nokia N97 ,
    而我想得到内容:
    浏览器:User-Agent=Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95_8GB/35.0.001; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413

  2. #2
    Super Contributor
    Join Date
    Nov 2005
    Posts
    1,968

    Re: how to get browser user agent

    SysUtil::UserAgentStringL(); 2.x版本

  3. #3
    Nokia Developer Champion
    Join Date
    May 2004
    Posts
    2,266

    Re: how to get browser user agent

    Hi,

    The following code should work on S60 5th Edition and may also work on 3rd Editoins.

    Code:
    ...
    const TInt KDefaultUserAgentString = 4;
    const TInt KMaxUserAgentStringLength = 256;
    HBufC8* ua = HBufC8::NewLC(KMaxUserAgentStringLength);
    TPtr8 ptr = ua->Des();
        const TUid KCRUidWebUtils = {0x101F8731}; 
        CRepository* cenrep = CRepository::NewLC(KCRUidWebUtils);
        TInt err = cenrep->Get(KDefaultUserAgentString, ptr); 
        User::LeaveIfError(err);
        CleanupStack::PopAndDestroy(cenrep);
        // here handle the user agent string
        ...
    CleanupStack::PopAndDestroy(ua);
    ...
    Reference:

    "CUserAgent::ReadUserAgentStringL()"
    http://opengrok.jpto.sonyericsson.ne...rAgent.cpp#220

    Regards

    Ziteng Chen

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    Re: how to get browser user agent

    感谢Ziteng,
    问题解决了,
    TInt err = cenrep->Get(KDefaultUserAgentString, ptr);
    中间的值从0取到7,就可以得到需要的字符串了。
    再次感谢!

  5. #5
    Nokia Developer Champion
    Join Date
    Jul 2007
    Location
    ShenZhen, China
    Posts
    4,346

    Re: how to get browser user agent

    这个方法有问题, ,我经过测试, 不能完美的拿到 user-agent字段,
    我使用如下代码
    void GetGwUA(TDes& aUA)
    {
    TBuf8<255> data;
    TBuf8<255> buf8;

    RFile f;
    User::LeaveIfError(f.Replace(CEikonEnv::Static()->FsSession(), _L("c:\\data\\quxy-ua.log"), EFileWrite));
    CleanupClosePushL(f);

    TInt major = 3;
    TInt minor = 0;
    SdkVer(major, minor);
    data.Format(_L8("major = %d, minor = %d\r\n"), major, minor);
    f.Write(data);

    CRepository* cenrep = CRepository::NewLC(TUid::Uid(0x101f8731));
    for ( TInt i = 0; i < 0xf; i++ )
    {
    TBuf<255> buf;
    TInt ret = cenrep->Get(i, buf);
    buf8.Copy(buf);
    data.Format(_L8("get %x: ret = %d, buf.Length() = %d, buf = %S\r\n"), i, ret, buf8.Length(), &buf8 );
    f.Write(data);

    switch (minor)
    {
    case 0:
    {
    if ( ret == KErrNone && buf.Length() > 0 && i < 5 )
    {
    aUA.Append(buf);
    if ( i < 4 )
    {
    aUA.Append(_L(" "));
    }
    }
    break;
    }

    case 1:
    case 2:

    default:
    break;
    }
    if ( minor < 2 && ret == KErrNone && buf.Length() > 0 && i < 5 )
    {
    if ( i < 4 )
    {
    aUA.Append(_L(" "));
    }
    }
    else
    if ( minor == 2 && i == 4 )
    {
    aUA = buf;
    }
    }

    buf8.Copy(aUA);
    data.Format(_L8("ua = %S\r\n"), &buf8 );
    f.Write(data);
    CleanupStack::PopAndDestroy(2);
    }
    但是每个机型上测试出来的结果, 不能组成完整的user0agent字段
    下面是 5630的
    major = 3, minor = 2
    get 0: ret = 0, buf.Length() = 14, buf = SymbianOS/9.3;
    get 1: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get 2: ret = 0, buf.Length() = 21, buf = Nokia5630d-1/011.020;
    get 3: ret = 0, buf.Length() = 39, buf = Profile/MIDP-2.1 Configuration/CLDC-1.1
    get 4: ret = 0, buf.Length() = 0, buf =
    get 5: ret = 0, buf.Length() = 75, buf = Nokia5630d-1/011.020; Series60/3.2.3 Profile MIDP2.1 Configuration/CLDC-1.1
    get 6: ret = 0, buf.Length() = 0, buf =
    get 7: ret = 0, buf.Length() = 0, buf =
    get 8: ret = 0, buf.Length() = 58, buf = "http://nds1.nds.nokia.com/uaprof/Nokia5630d-1r100-2G.xml"
    get 9: ret = 0, buf.Length() = 58, buf = "http://nds1.nds.nokia.com/uaprof/Nokia5630d-1r100-3G.xml"
    get a: ret = -1, buf.Length() = 0, buf =
    get b: ret = 0, buf.Length() = 20, buf = Nokia5630d-1/011.020
    get c: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get d: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.1
    get e: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    ua =
    实际是
    gw-ua is
    Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 Nokia5630d-1/011.020; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413


    下面是
    E75的
    major = 3, minor = 2
    get 0: ret = 0, buf.Length() = 14, buf = SymbianOS/9.3;
    get 1: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get 2: ret = 0, buf.Length() = 20, buf = NokiaE75-1/100.48.78
    get 3: ret = 0, buf.Length() = 39, buf = Profile/MIDP-2.1 Configuration/CLDC-1.1
    get 4: ret = 0, buf.Length() = 0, buf =
    get 5: ret = 0, buf.Length() = 74, buf = NokiaE75-1/100.48.78; Series60/3.2 Profile/MIDP-2.1 Configuration/CLDC-1.1
    get 6: ret = 0, buf.Length() = 0, buf =
    get 7: ret = 0, buf.Length() = 0, buf =
    get 8: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NE75-1r100.xml"
    get 9: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NE75-1r100.xml"
    get a: ret = -1, buf.Length() = 0, buf =
    get b: ret = 0, buf.Length() = 21, buf = NokiaE75-1/100.48.78;
    get c: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get d: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.1
    get e: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    ua =
    实际是
    gateway is
    Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1/100.48.78 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413

    这个是 N96的
    major = 3, minor = 2
    get 0: ret = 0, buf.Length() = 14, buf = SymbianOS/9.3;
    get 1: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get 2: ret = 0, buf.Length() = 16, buf = NokiaN96/12.043;
    get 3: ret = 0, buf.Length() = 39, buf = Profile/MIDP-2.1 Configuration/CLDC-1.1
    get 4: ret = 0, buf.Length() = 149, buf = NokiaN96/SymbianOS/9.3 Series60/3.2 Release/12.043; Mozilla/5.0 Profile/MIDP-2.1 Configuration/CLDC-1.1 AppleWebKit/413 (KHTML,like Gecko) Safari/413
    get 5: ret = 0, buf.Length() = 68, buf = NokiaN96/12.043; Series60/3.2 Profile MIDP2.1 Configuration/CLDC-1.1
    get 6: ret = 0, buf.Length() = 0, buf =
    get 7: ret = 0, buf.Length() = 0, buf =
    get 8: ret = 0, buf.Length() = 47, buf = "http://nds1.nds.nokia.com/uaprof/NN96r100.xml"
    get 9: ret = 0, buf.Length() = 47, buf = "http://nds1.nds.nokia.com/uaprof/NN96r100.xml"
    get a: ret = -1, buf.Length() = 0, buf =
    get b: ret = 0, buf.Length() = 15, buf = NokiaN96/12.043
    get c: ret = 0, buf.Length() = 12, buf = Series60/3.2
    get d: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.1
    get e: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    ua = NokiaN96/SymbianOS/9.3 Series60/3.2 Release/12.043; Mozilla/5.0 Profile/MIDP-2.1 Configuration/CLDC-1.1 AppleWebKit/413 (KHTML,like Gecko) Safari/413
    实际值相同

    这个是N95的
    major = 3, minor = 1
    get 0: ret = 0, buf.Length() = 12, buf = NokiaN95/1.0
    get 1: ret = 0, buf.Length() = 0, buf =
    get 2: ret = 0, buf.Length() = 37, buf = (20.1.001) SymbianOS/9.2 Series60/3.1
    get 3: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.0
    get 4: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    get 5: ret = 0, buf.Length() = 0, buf =
    get 6: ret = 0, buf.Length() = 0, buf =
    get 7: ret = 0, buf.Length() = 0, buf =
    get 8: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NN95-5r100.xml"
    get 9: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NN95-5r100.xml"
    get a: ret = -1, buf.Length() = 0, buf =
    get b: ret = 0, buf.Length() = 17, buf = NokiaN95/20.1.001
    get c: ret = 0, buf.Length() = 26, buf = SymbianOS/9.2 Series60/3.1
    get d: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.0
    get e: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    ua = NokiaN95/1.0 (20.1.001) SymbianOS/9.2 Series60/3.1 Profile/MIDP-2.0 Configuration/CLDC-1.1
    实际值相同

    这个是 N82的
    major = 3, minor = 1
    get 0: ret = 0, buf.Length() = 27, buf = Mozilla/5.0 (SymbianOS/9.2;
    get 1: ret = 0, buf.Length() = 12, buf = Series60/3.1
    get 2: ret = 0, buf.Length() = 18, buf = NokiaN82/31.0.016;
    get 3: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.0
    get 4: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    get 5: ret = 0, buf.Length() = 2, buf = U;
    get 6: ret = 0, buf.Length() = 0, buf =
    get 7: ret = 0, buf.Length() = 48, buf = ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
    get 8: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NN82-1r100.xml"
    get 9: ret = 0, buf.Length() = 49, buf = "http://nds1.nds.nokia.com/uaprof/NN82-1r100.xml"
    get a: ret = -1, buf.Length() = 0, buf =
    get b: ret = 0, buf.Length() = 18, buf = NokiaN82/31.0.016;
    get c: ret = 0, buf.Length() = 12, buf = Series60/3.1
    get d: ret = 0, buf.Length() = 16, buf = Profile/MIDP-2.0
    get e: ret = 0, buf.Length() = 22, buf = Configuration/CLDC-1.1
    ua = Mozilla/5.0 (SymbianOS/9.2; Series60/3.1 NokiaN82/31.0.016; Profile/MIDP-2.0 Configuration/CLDC-1.1
    实际值
    gateway ua is
    Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN82/31.0.016; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413

    即没有找到拼装的规律可以用某些字段拼装出来

    如果只是要机型信息, 用 CTelephony::GetPhoneId就可以了
    ----------------------------
    坚持学习, 坚持编码
    http://www.devdiv.net/
    qxiaoyuan

  6. #6
    Nokia Developer Champion
    Join Date
    May 2004
    Posts
    2,266

    Re: how to get browser user agent

    Hi,

    The code I provided is from Symbian^3, considering the bad reputation of backward compatibility of Symbian OS/S60 (and UIQ), I'm not supprised if it doesn't work on S60 3rd Editions.

    You can find the full logic of creating Symbian^3 browser user agnet string in the /s60/mw/web/BrowserUtilities/WebUtils/src/CUserAgnt.cpp, in brief the browser first read the value of key 4, cenrep 0x101F8731, if not empty then it is used as the UA,

    Code:
    ...
    void CUserAgent::ReadUserAgentStringL()
        {
        ...
        retCode = iRepository->Get(EDefaultUserAgentString, tempPtr);
        if(retCode != KErrNone)
            {
            ...
            User::Leave(retCode);
            }
        if(!IsUAStringEmpty(tempPtr))
            {
            ... 
            return;
            }
        ...
        ReadAndCreateUAStringL(tempPtr);
        ...
        }
    else the browser collects version/hardware info to format the UA,

    Code:
    ...
    // ---------------------------------------------------------
    // CUserAgent::ReadAndCreateUAStringL()
    // Reads verious fragments from OSExt APIs/CenRep and crates the UA string
    // ---------------------------------------------------------
    void CUserAgent::ReadAndCreateUAStringL(TPtr &aUAStringPtr)
        {
        GetFragmentsL();
    
        // Mozilla/5.0 (<Symbian Version> <Series60Version> <HardwareType> <MIDP_CLDC Version> ) AppleWebKit/413 (KHTML, like Gecko) BrowserName/Version
    
        aUAStringPtr.Copy(KPlaceHolderMozillaVer);
        aUAStringPtr.Append(KSpaceChar);
        ...
        }
    From your log it seems that the format of S60 3rd Editon FP2 browser user agent is only a little bit different ("U;" between <Symbian Version> and <SeriesS60Version>),

    Code:
    // Mozilla/5.0 (<Symbian Version> U; <Series60Version> <HardwareType> <MIDP_CLDC Version> ) AppleWebKit/413 (KHTML, like Gecko) BrowserName/Version
    Reference:

    "CUserAgent::ReadUserAgentStringL"
    http://developer.symbian.org/xref/ep...rAgent.cpp#195

    Regards

    Ziteng Chen

Similar Threads

  1. Retrieve Browser User-Agent
    By bgenady in forum Qt
    Replies: 3
    Last Post: 2010-07-14, 21:37
  2. changing S60 browser user agent via python
    By gontofe in forum Symbian
    Replies: 13
    Last Post: 2008-07-15, 14:49
  3. Modify user-agent header browser
    By felixksp in forum Symbian
    Replies: 2
    Last Post: 2008-02-14, 21:01
  4. User agent
    By sunil.dce in forum Mobile Java General
    Replies: 2
    Last Post: 2007-01-29, 08:16
  5. How to identify a WAP browser / phone - User agent field
    By frostyfone in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2001-11-08, 14:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •