goahead内部读取url键值代码
// test.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; #define CGI_MAX_SOCKET 256 #define CGI_MAX_BUF 1024 #define CGI_MAX_LEN 256 typedef int (*WebCallback)(char *output,char *input,unsigned char auth); typedef struct _CGISOCKET{ int socket; unsigned char buf[CGI_MAX_BUF]; char auth; }CGISOCKET,*PCGISOCKET; typedef struct _CGIWEBCMD{ int cmdindex; //cgi command index char key[32]; //cgi command string WebCallback callback; short p2pcmd; short other; }CGIWEBCMD,*PCGIWEBCMD; typedef struct _CGISETPARAM{ int cmdindex; //cgi command index char key[32]; //cgi command string int (*callback)(char *output,char *input,unsigned char auth); //cgi calllback short p2pcmd; //cgi command index short other; //cgi command index }CGISETPARAM,*PCGISETPARAM; static int GetStrParamValue(const char *purl, const char *pkey, char *pkeyvalue,int maxlen) { const char *phead; const char *pdst = purl; const char *pend = purl; char *presult = NULL; int len = 0; //find key // printf("purl:%s pkey:%s\n",purl,pkey); while(1){ phead = strstr(pdst, pkey); if (!phead) { return -1; } presult = (char*)(phead - 1); //split chart // printf("presult %x\n",presult[0]); if ((presult[0] == 0x3f) || (presult[0] == 0x26)) //0x3f->? 0x26->& { break; } pdst += strlen(pkey); } // printf("phead0:%s\n",phead); //move phead to start of value,eg:value=12345;,then move phead=12345; phead = phead + strlen(pkey) ; //find key end pend = strstr(phead, "&"); //if find end of key if (pend) { //find end of key len = pend - phead; if (len > maxlen){ len = maxlen; } memcpy(pkeyvalue, phead, len); //printf("phead1:%s pend:%s===len=%d\n",phead,pend,len); return 0; } //not find end of key,this is cgi end len = strlen(phead); if (len > maxlen){ len = maxlen; } memcpy(pkeyvalue, phead, len); //printf("keyvalue:%s phead2:%s\n",pkey,phead); return 0; } #define NON_NUM '0' static int hex2num(char c) { if (c>='0' && c<='9') return c - '0'; if (c>='a' && c<='z') return c - 'a' + 10; if (c>='A' && c<='Z') return c - 'A' + 10; return NON_NUM; } static int URLDecode(const char* str, const int strSize, char* result, const int resultSize) { char ch,ch1,ch2; int i; int j = 0;//record result index if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0)) { return 0; } for ( i=0; (i<strSize) && (j<resultSize); ++i) { ch = str[i]; switch (ch) { case '+': result[j++] = ' '; break; case '%': if (i+2<strSize) { ch1 = hex2num(str[i+1]);//high 4 bit ch2 = hex2num(str[i+2]);//low 4 bit if ((ch1!=NON_NUM) && (ch2!=NON_NUM)) result[j++] = (char)((ch1<<4) | ch2); i += 2; break; } else { break; } default: result[j++] = ch; break; } } result[j] = 0; return j; } int GetKeyValue(char *pparam,char *pkey,char *pvalue) { int iRet = -1; int len = strlen(pkey); char keyname[CGI_MAX_LEN]; char keyvalue[CGI_MAX_LEN]; char decoderbuf[CGI_MAX_LEN]; // printf("get key value=%d value:%s\n",len,pkey); // printf("pparam:%s pkey:%s pvalue:%s\n",pparam,pkey,pvalue); //check maxlen if( (!pparam) || (!pkey) || (!pvalue)){ return iRet; } memset(keyname,0x00,CGI_MAX_LEN); strcpy(keyname,pkey); keyname[len]='='; // printf("get key value1 pkey=%s\n",keyname); memset(keyvalue,0x00,CGI_MAX_LEN); iRet = GetStrParamValue(pparam, keyname, keyvalue,CGI_MAX_LEN - 1); if (iRet == 0x00){ memset(decoderbuf,0x00,CGI_MAX_LEN); URLDecode(keyvalue,CGI_MAX_LEN,decoderbuf,CGI_MAX_LEN-1); strcpy(pvalue,decoderbuf); // printf("pvalue:%s\n",pvalue); } // printf("get key value2=%d\n",iRet); return iRet; } int _tmain(int argc, _TCHAR* argv[]) { char value[100]; // GetStrParamValue("https://www.baidu.com/s?wd=error%20C2440%3A%20","wd",value,100); GetKeyValue("https://www.baidu.com/s?wd=error%20C2440%3A%20","wd",value); cout<<value<<endl; system("PAUSE"); return 0; }
输出:error C2440:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-03-02 spdx许可
2022-03-02 mtk ralink
2022-03-02 GCT Semiconductor
2022-03-02 mtk
2022-03-02 qt vs配置
2022-03-02 工业4g 5g 互联网
2022-03-02 QMovie gif播放类