免费教程_免费网赚教程_破解版软件-寂涯网络学习基地

当前位置: 主页 > 系统综合 > 各类编程 > 如何使用Google Url安全解析URL(2)

如何使用Google Url安全解析URL(2)

时间:2011-12-12 16:13来源:未知 整理:寂涯网络 点击:

");
这样我们只需要直接传入URL 字串即可。
接下来它会调用InitCanonical函数进行下一步的解析:   
 
template<typename STR>
bool InitCanonical(const STR& input_spec,
                   std::string* canonical,
                   url_parse::Parsed* parsed) {
  // Reserve enough room in the output for the input, plus some extra
so that
  // we have room if we have to escape a few things without reallocating.
  canonical->reserve(input_spec.size() + 32);
  url_canon::StdStringCanonOutput output(canonical);
  bool success = url_util::Canonicalize(
      input_spec.data(), static_cast<int>(input_spec.length()),
      NULL, &output, parsed);
  output.Complete();  // Must be done before using string.
  return success;

  在函数InitCanonical中,调用了url_util命名空间的Canonicalize( ->
DoCanonicalize)进行解析,主要的解析过程在url_parse中完成。
  了解了解析过程,我们用起来也就很快了,其实也没什么难度,就是几个方法,挺简单
的:
  以下的一些方法是判断解析的URL中是否包含这些元素:
 

以下的操作就是得到这些元素: 

std::string scheme() const
 std::string username() const
 std::string password() const
 std::string host() const
 std::string port() const
 std::string path() const
 std::string query() const
  std::string ref() const

另外,GURL还支持直接转换成string,这样就可以直接输出URL:std::cout << gurl;
我们现在来看看具体的使用代码吧: 

#include "googleurl\src\gurl.h"
int _tmain(int argc, _TCHAR* argv[])
{
  //GoogleUrl
 GURL
gurl("http://user:123456@foo.tengattack.com:8080/index.php?action=i
nit");
  std::cout << std::endl <<  "URL: "  << gurl << std::endl << std::endl;
  //得到域名\主机地址
  std::cout << "host() -> " << gurl.host() << std::endl;
  //得到端口号
  if (gurl.has_port())
   std::cout << "port() -> " << gurl.port() << std::endl;
  //得到query
  if (gurl.has_query())
   std::cout << "query() -> " << gurl.query() << std::endl;
  //得到用户名密码
  if (gurl.has_username())
   std::cout <<  "username() -> "  << gurl.username() << std::endl;
  if (gurl.has_password())
   std::cout <<  "password() -> "  << gurl.password() << std::endl;
  //得到文件名  

  std::cout << "ExtractFileName() -> " << gurl.ExtractFileName() <<
std::endl;
  //这个也是端口号,与port()的区别就是就算没有指定端口号默认会返回
HTTP 80端口
  std::cout << "EffectiveIntPort() -> " << gurl.EffectiveIntPort()
<< std::endl;
  //判断域名是否为IP地址
  std::cout << "HostIsIPAddress() -> " << gurl.HostIsIPAddress() <<
std::endl;
 std::cin.get();
  return 0;

输出如下: 

cmd输出框

  看,很方便的就把所需要的数据解析出来了吧。
  从 GoogleUrl 这种小的项目中可以看到 Google 开发人员的严谨的态度,而这种态度也
同样是我们最需要学习的。我们更需要在我们开发的工具中注意一下一些小问题,往往一个
不起眼的问题也可以酿成大祸。千里之堤,毁于蚁穴啊。 
  
 
 
 
 
 
 
 
 
 

本页地址 http://www.jybase.net/biancheng/20111212709.html

百度搜索更多

谷歌搜索更多

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------

评价:
昵称: 验证码:点击我更换图片
推荐内容
赞助商
赞助商


关于本站免责声明视频更新google百度地图视频地图RRS订阅

如有什么问题请在本站留言,或发邮件到 hxt167#foxmail.com