日期:2014-05-17  浏览次数:20673 次

为什么程序中获取百度搜索引擎的关键字是乱码?
我做了一个简单的测试,当在百度上输入关键字是中文时,程序中的到的关键字为什么是乱码?
1。aa.html
<script language='javascript' type='text/javascript' src='http://localhost:3514/UTF8toGB2312/GetMakeCode.aspx' charset='utf-8'></script>

2。GetMakeCode.aspx

protected void Page_Load(object sender, EventArgs e)
  {
  Response.Expires = 0;
  Response.CacheControl = "no-cache";

  if (!this.IsPostBack)
  {
  StringBuilder strTemp = new StringBuilder();
  strTemp.Append("var referrer = escape('http://www.baidu.com/s?wd=%BC%B4%CA%B1%CD%A8');\r\n");
  //http://www.baidu.com/s?wd=%BC%B4%CA%B1%CD%A8
  //http://www.google.cn/search?hl=zh-CN&q=%E5%8D%B3%E6%97%B6%E9%80%9A&meta=&aq=f&oq=
  //http://www.baidu.com/s?wd=www.wangyetong.com
  strTemp.Append("alert(referrer);");
  strTemp.Append("var scriptcode = \"<iframe frameborder='no' border='0' marginwidth='0' marginheight='0' scrolling='no' src='http://localhost:3514/UTF8toGB2312/SaveScript.aspx?referrer=\"+referrer+\"' style=\'display:none;'></iframe>\"\r\n");

  strTemp.Append("document.write(scriptcode);\r\n");

  Response.ContentType = "text/plain";

  Response.Write(strTemp.ToString());
  }
  }

3。saveScript.aspx中

<script src="JS/UrlBuilder.js" type="text/javascript" language="javascript"></script>
<script language="javascript" type="text/javascript">
  
  window.onload = function doSearch()
  {
  var refer = unescape('<%=Request.QueryString["referrer"] %>'); 
  alert(refer);
  var url = new UrlBuilder(refer);
  var shortUrl = refer.toLowerCase();
  var keywords='';
  if(shortUrl =='')
  {
  keywords='0';
  }
  else
  {
  if(shortUrl.indexOf('baidu.com')>0||shortUrl.indexOf('baidu.cn')>0) 
  {
  keywords=url.GetValue('wd','gb2312');
  }
  else if(shortUrl.indexOf('google.com')>0||shortUrl.indexOf('google.cn')>0) 
  {
  keywords=url.GetValue('q','UTF8');
  }
  else
  {
  keywords='1';
  }
  }
   
  alert(keywords);
   
  window.open('http://localhost:3514/UTF8toGB2312/SaveGuestLogs.aspx?referrer='+escape(keywords)+'');  
  }
   
</script>

4。saveScriptLogs.aspx中
 <script language="javascript" type="text/javascript">
  alert("aaaaaaaaaaaaaa");
  var referror = '<%=Request.QueryString["referrer"] %>';
  // var referror = '<%=refer %>';
  alert(referror);
  </script>

5。URLBuilder.js

// JScript 文件

  function UrlBuilder(url)
  {
  this.m_Href = null;
  this.m_Host = null;
  this.m_Hostname = null; 
  this.m_Port = null;