隨筆-348  評論-598  文章-0  trackbacks-0

          OPHONE獲得USER AGENT[權(quán)威解決方案]

          論壇里有幾個人發(fā)了一些代碼,如何獲得UA,但是自己都實(shí)踐過了一遍,一個都不行。最近做OPHONE的項(xiàng)目,跟移動終端的人打了不少交道,了解了一些相關(guān)知識,這里把權(quán)威的解決方案發(fā)布出來。 我不是方法的發(fā)明者,但是我是方法的實(shí)踐者。順便也在這里感謝相關(guān)的人。 

          原理: 

          OPHONE的UA存放位置: 
          1)OPHONE 1.0和1.5     存放于/opl/etc/properties.xml 
          1)OPHONE 2.0     存放于/opl/etc/product_properties.xml 

          大家可以通過下面的步驟自己查看: 
          1),連上手機(jī),或者模擬器。 
          2),輸入 adb shell 
          3),輸入 cd opl 
          4),輸入 cd etc 
          5),輸入 cat properties.xml (或者cat product_properties.xml 【OPHONE2.0】) 

          結(jié)果如下圖: 
           

          以上就是properties.xml的內(nèi)容,接下來就是獲得這個UA,加到自己的聯(lián)網(wǎng)請求里去。 
          我自己寫了一個,適用于目前3個版本的OPHONE。 

          AndroidPlatform.java 

          package com.***.****; 

          import java.io.ByteArrayOutputStream; 
          import java.io.File; 
          import java.io.FileInputStream; 


          public class AndroidPlatform  { 
                  
                  public static final String KEYSTRING_USER_AGENT = "user_agent_key"; 
                  
                  public static String getUAFromProperties() 
                  { 
                          try { 
                          FileInputStream is = getPropertyStream(); 
                          ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); 
                                  byte buf[] = new byte[1024]; 
                                  for(int k = 0; -1 != (k = is.read(buf));) 
                             bytearrayoutputstream.write(buf, 0, k); 
                                  
                                  String fileString = new String(bytearrayoutputstream.toByteArray(), "UTF-8"); 
                                  
                                  return getProperties(KEYSTRING_USER_AGENT, fileString); 
                                  
                          //System.out.println("IS FILE Android Platform  " + bytearrayoutputstream.size() +  "  "+()); 
                    
                          } catch (Exception e) { 
                                  // TODO: handle exception 
                                  
                                  System.out.println("IS FILE erororo"); 
                                  e.printStackTrace(); 
                          } 
                          return null; 
                  } 
                  
                  
                  
                  public static FileInputStream getPropertyStream() 
                  { 
                          try { 
                                  
                                  File property = new java.io.File("/opl/etc/properties.xml"); 
                                  if(property.exists()) 
                                  { 
                                          return new FileInputStream(new java.io.File("/opl/etc/properties.xml")); 
                                  } 
                                  else 
                                  { 
                                          property = new java.io.File("/opl/etc/product_properties.xml"); 
                                          if(property.exists()) 
                                          { 
                                                  return new FileInputStream(new java.io.File("/opl/etc/product_properties.xml")); 
                                          } 
                                          else 
                                          { 
                                                  return null; 
                                          } 
                                  } 
                    
                          } catch (Exception e) { 
                                  // TODO: handle exception 
                                  e.printStackTrace(); 
                          } 
                          return null; 
                  } 
                  
                  
                  public static String getProperties(String key, String content) 
                  { 
                          String STARTKEY = "<"+key+">"; 
                          String ENDKEY = "</"+key+">"; 
                          content = content.replace("\r", ""); 
                          content = content.replace("\n", ""); 
                          
                          int startIndex = content.indexOf(STARTKEY) + STARTKEY.length(); 
                          int endIndex = content.indexOf(ENDKEY); 
                          if(startIndex > -1 && endIndex > -1) 
                          { 
                                  return content.substring(startIndex, endIndex); 
                          } 
                          else 
                          return null; 
                  } 

                  


          聯(lián)網(wǎng)請求時,加入UA即可,這樣就做到了自動適配了。具體如下: 

          private int CountMoneyCMWAPNEWWAY(String urlstr) 
            { 

                    String strHead = ""; 
                    try{ 
                            if(!GameLet._self.isNetworkCMWAPAvailable()) 
                            { 
                                    GameLet._self.ActiveNetWorkByMode("wap"); 
                                    Thread.sleep(5000); 
                            } 

                        int splashIndex = urlstr.indexOf("/", 7); 

                        String hosturl = urlstr.substring(7, splashIndex); 
                        String hostfile = urlstr.substring(splashIndex); 
                        
                       

                        HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http"); 
                        HttpHost target = new HttpHost(hosturl, 80, "http");         
                    
                        HttpParams httpParams = new BasicHttpParams(); 
                        HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000); 
                        HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000); 
                        HttpConnectionParams.setSocketBufferSize(httpParams, 8192); 
                        HttpClientParams.setRedirecting(httpParams, true); 
                    
                        String userAgent =  AndroidPlatform.getUAFromProperties(); 
                    
                        HttpProtocolParams.setUserAgent(httpParams, userAgent); 
                        DefaultHttpClient httpclient = new DefaultHttpClient(httpParams); 

                        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 
                          
                        HttpGet req = new HttpGet(hostfile); 
                     
                        HttpResponse rsp = httpclient.execute(target, req); 
                       
                        HttpEntity entity = rsp.getEntity(); 
                        
                        InputStream inputstream = entity.getContent(); 
                        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); 
                        byte abyte1[] = new byte[1024]; 
                        for(int k = 0; -1 != (k = inputstream.read(abyte1));) 
                           bytearrayoutputstream.write(abyte1, 0, k); 
                        
                        strHead = new String(bytearrayoutputstream.toByteArray(), "UTF-8"); 
                        
                        httpclient.getConnectionManager().shutdown();   
                        
                        } 
                        catch (Exception e)        { 
                                return 2; 
                        } 
                         
                      if(strHead.indexOf("status=1301") > -1 || strHead.indexOf("status=1300") > -1) 
                      { 
                            return 1; 
                      } 
                      else 
                      { 
                            return 0; 
                      } 
                 
            }


          ---------------------------------------------------------
          專注移動開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2010-08-24 15:17 TiGERTiAN 閱讀(709) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 夹江县| 城口县| 东光县| 伽师县| 施甸县| 安陆市| 丰镇市| 彭泽县| 凯里市| 瓮安县| 申扎县| 诸城市| 静乐县| 榆林市| 临武县| 平湖市| 卓尼县| 丹江口市| 东明县| 双江| 民和| 大名县| 灵璧县| 应城市| 兴义市| 山阳县| 手游| 澎湖县| 滦平县| 灵丘县| 板桥市| 辽阳市| 吕梁市| 龙游县| 来安县| 安远县| 木兰县| 偏关县| 建始县| 台安县| 卓尼县|