怎么样从URL中分解出域名来?急,在线等 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用这个:gethostbyaddr();struct HOSTENT* FAR gethostbyaddr( const char* addr, int len, int type); #include <iostream>#include <string>std::string getdomain(const std::string& url){ std::string::size_type pos1 = url.find("://"); if(pos1 == std::string::npos) pos1 = -3; std::string::size_type pos2 = url.find('/', pos1 + 3); if(pos2 == std::string::npos) pos2 = url.size(); return url.substr(pos1 + 3, pos2 - pos1 - 3);}int main(){ std::cout<<getdomain("http://www.abc.com/h.htm")<<std::endl; std::cout<<getdomain("www.abc.com")<<std::endl; std::cout<<getdomain("http://www.abc.com")<<std::endl; std::cout<<getdomain("ftp://www.abc.com/123/4545/1.asp")<<std::endl; std::cout<<getdomain("ftp://www.abc.com/abc.rar")<<std::endl; std::cout<<getdomain("www.abc.com/111/222/abc.com")<<std::endl;} 如在Windows系统下,可以简单写为:TCHAR szDomain[MAX_PATH];DWORD dwLen=sizeof(szDomain)/sizeof(szDomain[0]);HRESULT hr=UrlGetPart(_T("http://www.yahoo.com/finanice/index.html"), szDomain, &dwLen,URL_PART_HOSTNAME,0); 标 题: URL分解类发信站: 荔园晨风BBS站 (2005年05月28日21:46:13 星期六), 站内信件//by bigcat.//URLParser.h//--------------------------------------------------------------------------#pragma onceclass CURLParser{public: CURLParser(void); ~CURLParser(void);protected: char m_szProtocol[20]; char m_szHost[200]; int m_nPort; char m_szPath[200];private: // 重置环境 void Reset(void);public: // 分析URL bool Parse(char *szURL); void getHost( char *szHost ) { strcpy( szHost, m_szHost); } void getPath( char *szPath ) { strcpy( szPath, m_szPath); } void getProtocol( char *szProtocol ) { strcpy( szProtocol, m_szProtocol ); } int getPort() { return m_nPort; }};/////////////////////////////////////////////////////////////////////////// URLParser.CPP// By Bigcat.#include "StdAfx.h"#include ".\urlparser.h"CURLParser::CURLParser(void){ Reset();}CURLParser::~CURLParser(void){}// 重置环境void CURLParser::Reset(void){ memset( m_szProtocol, 0, sizeof( m_szProtocol ) ); memset( m_szHost, 0, sizeof( m_szHost ) ); memset( m_szPath, 0, sizeof( m_szPath ) ); strcpy( m_szPath, "/" ); m_nPort = 80;}// 分析URLbool CURLParser::Parse(char *szURL){ Reset(); char szTemp[200] = ""; char szPort[10] = ""; char *pStartPos = NULL; char *pEndPos = NULL; int startPos = 0; int endPos = 0; strcpy( szPort, "80" ); pStartPos = strstr( szURL, "//" ); endPos = (int)(pStartPos - szURL); strncpy( m_szProtocol, szURL, endPos - 1 ); pEndPos = strstr( szURL + endPos + 2, "/" ); pStartPos += 2; if ( pEndPos == NULL ) { // 后面部分全为主机 strncpy( szTemp, pStartPos, strlen(szURL) - (pStartPos - szURL) ); }else{ // 后面包含路径 strncpy( szTemp, pStartPos, pEndPos - pStartPos ); strncpy( m_szPath, pEndPos , strlen(szURL) - (pEndPos - szURL) ); } pEndPos = strstr( szTemp, ":" ); if ( pEndPos != NULL ) { // 指定端口 strncpy( m_szHost, szTemp, (int)(pEndPos - szTemp) ); strcpy( szPort, pEndPos + 1 ); }else{ // 默认端口 80 strcpy( m_szHost, szTemp ); } m_nPort = atoi( szPort ); return true;}///////////////////////////////////////////////////////////////////// demo.cpp// 演示用法#include "URLParser.h"int _tmain(int argc, _TCHAR* argv[]){ CURLParser myURL; char szTemp[300] = ""; myURL.Parse("http://www.163.com/default.asp"); printf("URL: http://www.163.com/default.asp\n" ); myURL.getProtocol( szTemp ); printf("Protocol: %s\n", szTemp); myURL.getHost( szTemp ); printf("Host: %s\n", szTemp); printf("Port: %d\n", myURL.getPort() ); myURL.getPath( szTemp ); printf("Path: %s\n", szTemp); printf("\n"); myURL.Parse("ftp://www.qq.com:8081/service/request.htm"); printf("URL: ftp://www.qq.com:8081/service/request.htm\n" ); myURL.getProtocol( szTemp ); printf("Protocol: %s\n", szTemp); myURL.getHost( szTemp ); printf("Host: %s\n", szTemp); printf("Port: %d\n", myURL.getPort() ); myURL.getPath( szTemp ); printf("Path: %s\n", szTemp); return 0;} 严重重绘闪烁问题 如何设计自定义解释器 MSFlexGrid这个控件如何 vc用ODBC访问SQL server数据库,修改的结果为何在数据库的表中没有显示? 高手请入,CRichEditCtrl如何显示RTF图片问题?解决问题另外开贴给分 一个简单的问题 提供一些好书下载!全都是精典哦! 关于单文档中的菜单问题,高手请进! 我所建的一个工程因为磁盘错误导致类信息丢失 CTreeCtrl消息的问题 关于经纬度的问题 禁用VC编辑框
const char* addr,
int len,
int type
);
#include <string>std::string getdomain(const std::string& url)
{
std::string::size_type pos1 = url.find("://");
if(pos1 == std::string::npos) pos1 = -3;
std::string::size_type pos2 = url.find('/', pos1 + 3);
if(pos2 == std::string::npos) pos2 = url.size();
return url.substr(pos1 + 3, pos2 - pos1 - 3);
}int main()
{
std::cout<<getdomain("http://www.abc.com/h.htm")<<std::endl;
std::cout<<getdomain("www.abc.com")<<std::endl;
std::cout<<getdomain("http://www.abc.com")<<std::endl;
std::cout<<getdomain("ftp://www.abc.com/123/4545/1.asp")<<std::endl;
std::cout<<getdomain("ftp://www.abc.com/abc.rar")<<std::endl;
std::cout<<getdomain("www.abc.com/111/222/abc.com")<<std::endl;
}
DWORD dwLen=sizeof(szDomain)/sizeof(szDomain[0]);HRESULT hr=UrlGetPart(_T("http://www.yahoo.com/finanice/index.html"), szDomain,
&dwLen,URL_PART_HOSTNAME,0);
发信站: 荔园晨风BBS站 (2005年05月28日21:46:13 星期六), 站内信件//by bigcat.//URLParser.h
//--------------------------------------------------------------------------
#pragma onceclass CURLParser
{
public:
CURLParser(void);
~CURLParser(void);protected: char m_szProtocol[20];
char m_szHost[200];
int m_nPort;
char m_szPath[200];private:
// 重置环境
void Reset(void);
public:
// 分析URL
bool Parse(char *szURL); void getHost( char *szHost )
{
strcpy( szHost, m_szHost);
} void getPath( char *szPath )
{
strcpy( szPath, m_szPath);
} void getProtocol( char *szProtocol )
{
strcpy( szProtocol, m_szProtocol );
} int getPort()
{
return m_nPort;
}};
/////////////////////////////////////////////////////////////////////////
// URLParser.CPP
// By Bigcat.
#include "StdAfx.h"
#include ".\urlparser.h"CURLParser::CURLParser(void)
{
Reset();
}CURLParser::~CURLParser(void)
{
}// 重置环境
void CURLParser::Reset(void)
{
memset( m_szProtocol, 0, sizeof( m_szProtocol ) );
memset( m_szHost, 0, sizeof( m_szHost ) );
memset( m_szPath, 0, sizeof( m_szPath ) );
strcpy( m_szPath, "/" );
m_nPort = 80;
}// 分析URL
bool CURLParser::Parse(char *szURL)
{
Reset(); char szTemp[200] = "";
char szPort[10] = "";
char *pStartPos = NULL;
char *pEndPos = NULL;
int startPos = 0;
int endPos = 0;
strcpy( szPort, "80" ); pStartPos = strstr( szURL, "//" );
endPos = (int)(pStartPos - szURL);
strncpy( m_szProtocol, szURL, endPos - 1 ); pEndPos = strstr( szURL + endPos + 2, "/" );
pStartPos += 2;
if ( pEndPos == NULL )
{
// 后面部分全为主机
strncpy( szTemp, pStartPos, strlen(szURL) - (pStartPos - szURL) ); }else{
// 后面包含路径
strncpy( szTemp, pStartPos, pEndPos - pStartPos );
strncpy( m_szPath, pEndPos , strlen(szURL) - (pEndPos - szURL) );
} pEndPos = strstr( szTemp, ":" );
if ( pEndPos != NULL )
{
// 指定端口
strncpy( m_szHost, szTemp, (int)(pEndPos - szTemp) );
strcpy( szPort, pEndPos + 1 );
}else{
// 默认端口 80
strcpy( m_szHost, szTemp );
}
m_nPort = atoi( szPort ); return true;
}
///////////////////////////////////////////////////////////////////// demo.cpp
// 演示用法#include "URLParser.h"int _tmain(int argc, _TCHAR* argv[])
{ CURLParser myURL;
char szTemp[300] = ""; myURL.Parse("http://www.163.com/default.asp");
printf("URL: http://www.163.com/default.asp\n" );
myURL.getProtocol( szTemp );
printf("Protocol: %s\n", szTemp);
myURL.getHost( szTemp );
printf("Host: %s\n", szTemp);
printf("Port: %d\n", myURL.getPort() );
myURL.getPath( szTemp );
printf("Path: %s\n", szTemp); printf("\n"); myURL.Parse("ftp://www.qq.com:8081/service/request.htm");
printf("URL: ftp://www.qq.com:8081/service/request.htm\n" );
myURL.getProtocol( szTemp );
printf("Protocol: %s\n", szTemp);
myURL.getHost( szTemp );
printf("Host: %s\n", szTemp);
printf("Port: %d\n", myURL.getPort() );
myURL.getPath( szTemp );
printf("Path: %s\n", szTemp);
return 0;}