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

 我来答
huanglenzhi
2015-04-24 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517184
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  论坛里有几个人发了一些代码,如何获得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 = "</"+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;
  }

  }

  联网请求时,加入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;
  }
  
  }
转载
微测检测
2023-10-30 广告
深圳市微测检测有限公司始创于2005年,是一家综合性、全方位、一站式的权威第三方检测认证公司。自成立以来,Microtest微测检测已成功为上万家企业完成数十万计的产品测试和认证,协助企业的产品畅销全球。Microtest微测检测已建立二十... 点击进入详情页
本回答由微测检测提供
ruyuetian
2018-03-17 · TA获得超过1.1万个赞
知道大有可为答主
回答量:9665
采纳率:98%
帮助的人:946万
展开全部
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 = "</"+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;
}

}

联网请求时,加入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;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dasfsd4423
2018-03-16 · 超过57用户采纳过TA的回答
知道小有建树答主
回答量:99
采纳率:94%
帮助的人:19.2万
展开全部
论坛里有几个人发了一些代码,如何获得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 = "</"+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;
}

}

联网请求时,加入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;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式