GetMidStr c++

2021-05-07 10:28

阅读:412

标签:字符   detail   取字符串   host   win   ash   dll   test   art   

假如一个CString中保存了一个网页的源码代码,那么我们如何获取我们想要的信息呢。

可以通过这个函数来截取字符串。

CString GetMidStrByLAndR(CString& strSrc, CString strLeft, CString strRight)
{
        CString strRet;
        int eIdxBegin = strSrc.Find(strLeft);
        if(eIdxBegin != -1)
        {
                eIdxBegin += strLeft.GetLength();
                int eIdxEnd = strSrc.Find(strRight, eIdxBegin);
                if (eIdxEnd != -1)
                {
                        strRet = strSrc.Mid(eIdxBegin, eIdxEnd-eIdxBegin);
                        return strRet;
                }
        }


        return strRet;
}

例如:

  CString str;//假如里面已经保存了html源码html里面有这么一段:

调用格式如下:

CString ReStr=GetMidStrByLAndR(str,L"input name=user_name value=\"",L"\"");

这个时候 ReStr里面的值就是 test_user



 

 

https://blog.csdn.net/qq125096885/article/details/50829192?locationNum=5

 

获得本机或指定IP的地理位置


#include
#import "C:\\Windows\\System32\\WinHttp.dll" no_namespace

void GetMidStrByLAndR(char* strSrc, char* strLeft, char* strRight,char strLocation[64])
{
char *eIdxBegin=strstr(strSrc,strLeft);
if(eIdxBegin != NULL)
{
eIdxBegin += strlen(strLeft);
char* eIdxEnd=strstr(eIdxBegin+1,strRight);
if (eIdxEnd != NULL)
memcpy(strLocation,eIdxBegin,eIdxEnd-eIdxBegin);
}
return ;
}


void GetIpPlace(char* strIp,char strLocation[64])
{

CoInitialize(NULL);
HRESULT hr = S_FALSE;
IWinHttpRequestPtr pHttpReq = NULL;
hr = pHttpReq.CreateInstance(__uuidof(WinHttpRequest));
if(FAILED(hr))
{
CoUninitialize();
return ;
}

char queryUrl[64]={0}, hostString[64]={0};

if (strIp==NULL)
{
memcpy(queryUrl,TEXT("http://1111.ip138.com/ic.asp"),ARRAYSIZE(TEXT("http://1111.ip138.com/ic.asp")));
memcpy(hostString,TEXT("1111.ip138.com"),ARRAYSIZE(TEXT("1111.ip138.com")));
}
else
{
_snprintf_s(queryUrl,ARRAYSIZE(queryUrl),("http://www.ip138.com/ips138.asp?ip=%s&action=2"),strIp);
memcpy(hostString,TEXT("www.ip138.com"),ARRAYSIZE(TEXT("www.ip138.com")));
}

hr = pHttpReq->Open(("GET"), (LPCTSTR)queryUrl);
if(FAILED(hr))
{
CoUninitialize();
return ;
}

pHttpReq->SetRequestHeader(("Accept"), ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
pHttpReq->SetRequestHeader(("Accept-Language"), ("zh-CN, en-US"));
pHttpReq->SetRequestHeader(("User-Agent"), ("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"));
pHttpReq->SetRequestHeader(("Host"), (LPCTSTR)hostString);
pHttpReq->SetRequestHeader(("Connection"), ("keep-alive"));

hr = pHttpReq->Send();
if(FAILED(hr))
{
CoUninitialize();
return ;
}

char *strContent=NULL;
_variant_t varRspBody = pHttpReq->GetResponseBody();
ULONG dataLen = varRspBody.parray->rgsabound[0].cElements;
char *pContentBuffer = (char *)varRspBody.parray->pvData;
strContent = pContentBuffer;

if (strIp==NULL)
{
GetMidStrByLAndR(strContent, ("来自:"), (""),strLocation);
//GetMidStrByLAndR(strContent, _T("您的IP是:["), _T(""),strLocation);
}
else
{
GetMidStrByLAndR(strContent, ("

  • 本站主数据:"),("
  • "),strLocation);
    //GetMidStrByLAndR(strContent, _T("
    • 您查询的IP:"), _T("
    • "),strLocation);
      }

      CoUninitialize();
      }


      int main()
      {
      char strLocation[64]={0};
      GetIpPlace(NULL,strLocation);
      printf("%s\n",strLocation);

      GetIpPlace("121.205.57.99",strLocation);
      printf("%s\n",strLocation);

      return 0;

      ————————————————
      版权声明:本文为CSDN博主「125096」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
      原文链接:https://blog.csdn.net/qq125096885/java/article/details/50829192

      GetMidStr c++

      标签:字符   detail   取字符串   host   win   ash   dll   test   art   

      原文地址:https://www.cnblogs.com/ruingking/p/13185457.html


评论


亲,登录后才可以留言!