永发信息网

如何获得Android的USER Agent-Android开发问答

答案:3  悬赏:40  手机版
解决时间 2021-02-13 16:23
如何获得Android的USER Agent-Android开发问答
最佳答案
论坛里有几个人发了一些代码,如何获得UA,但是自己都实践过了一遍,一个都不行。最近做OPHONE的项目,跟移动终端的人打了不少交道,了解了一些相关知识,这里把权威的解决方案发布出来。 我不是方法的发明者,但是我是方法的实践者。顺便也在这里感谢相关的人。

  原理:

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

  大家可以通过下面的步骤自己查看:
  1),连上手机,或者模拟器。
  2),输入 adb shell
  3),输入 cd opl
  4),输入 cd etc
  5),输入 cat properties.xml (或者cat product_properties.xml 【OPHONE2.0】)

  结果如下图:
  ophone.png (6 KB)
  ophone ua

  2010-08-24 14:09

  以上就是properties.xml的内容,接下来就是获得这个UA,加到自己的联网请求里去。
  我自己写了一个,适用于目前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 = "";
  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;
  }

  }

  联网请求时,加入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;
  }
  
  }
转载
全部回答
论坛里有几个人发了一些代码,如何获得UA,但是自己都实践过了一遍,一个都不行。最近做OPHONE的项目,跟移动终端的人打了不少交道,了解了一些相关知识,这里把权威的解决方案发布出来。 我不是方法的发明者,但是我是方法的实践者。顺便也在这里感谢相关的人。
原理:
OPHONE的UA存放位置:
1)OPHONE 1.0和1.5 存放于/opl/etc/properties.xml
1)OPHONE 2.0 存放于/opl/etc/product_properties.xml
大家可以通过下面的步骤自己查看:
1),连上手机,或者模拟器。
2),输入 adb shell
3),输入 cd opl
4),输入 cd etc
5),输入 cat properties.xml (或者cat product_properties.xml 【OPHONE2.0】)
结果如下图:
ophone.png (6 KB)
ophone ua
2010-08-24 14:09
以上就是properties.xml的内容,接下来就是获得这个UA,加到自己的联网请求里去。
我自己写了一个,适用于目前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 = "";
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;
}
}
联网请求时,加入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;
}
}
OPHONE的UA存放位置:
1)OPHONE 1.0和1.5 存放于/opl/etc/properties.xml
1)OPHONE 2.0 存放于/opl/etc/product_properties.xml
大家可以通过下面的步骤自己查看:
1),连上手机,或者模拟器。
2),输入 adb shell
3),输入 cd opl
4),输入 cd etc
5),输入 cat properties.xml (或者cat product_properties.xml 【OPHONE2.0】)
结果如下图:
ophone.png (6 KB)
ophone ua
2010-08-24 14:09
以上就是properties.xml的内容,接下来就是获得这个UA,加到自己的联网请求里去。
我自己写了一个,适用于目前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 = "";
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;
}
}
联网请求时,加入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;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
悠活网咖(绿色店)地址在哪,我要去那里办事
朋友好几年不见,偶尔的相遇会不会形同陌生人
我心律不齐,干体力活受不了,领导又不照顾,
多兰网咖地址在什么地方,想过去办事
lne2等于多少 数学
【阴平阳平上声去声】阴平指什么声.阳平指什
车灯太多无从下手?让我教你认仪表指示灯
冻美人养生之家地址有知道的么?有点事想过去
毛晓彤在新天龙八部第几出现第几消失
真的有八字相克这一说法吗
UL产品中什么是CRU产品CRU一定要有制造日期吗
解释数SQL面的 select * from a, b 是什
潘多拉网络会所(云集路店)怎么去啊,我要去那
江苏省工程地质勘察院张家港分院这个地址在什
农业生产的区域化是指()。A.每一个农业劳动组
推荐资讯
电脑看不见屏幕,只听得到声音,怎么办啊
阅读图文资料,完成下列要求。青海湖,地处青
菱排线/S208(路口)在哪里啊,我有事要去这个
怎么用三角尺画角?我需要步骤
下列何方的药物配伍,体现了标本兼顾,上下并
跟浮萍有关的诗句
【2014-9-19】2014年9月19日,怎么用英语翻译
可乐瓶可以做什么,废物利用
河南省鹤林机械公司怎么去啊,有知道地址的么
中国邮政(南丰邮政所)地址在什么地方,我要处
樱花国际日语(北京西城区)怎么去啊,我要去那
我在中国黄金买了一个手镯 只写了Hs千足金、
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?