WinInet以post方式发送数据包的问题

tommyjohn 发布于 2017/06/13 17:49
阅读 126
收藏 0

最近在自学,基础不太好,求大神帮忙看下,最好说的详细点。

在网上看到一个WinInet发送http请求的例子,但是有些地方不太懂,代码如下:

#include <Windows.h>
#include <WinInet.h>
#include <stdio.h>

#pragma comment(lib, "WinInet.lib")

int main()
{
    /*
    打开网页adresrchcache82/solr/select?indent=on&version=2.2&q=0P00000UG6+0P0000GLUK+157498+FSGBR04KHF%70L00000122+FSUSA04CAR+0P0000J395+FSUSA08H7B%70L00000138+0P00000F5D+558658+F0CAN062AS%70L00000138%7CU%24%24%24%24%24GBL+FSUSA0BDMH%70L00000204+F0000007T0+0P00000IH1+0P00001KTE+FSUSA0APV9%70L00000122+528765+0P00007XVN+F000000L2B%70L00000138%7CU%24%24%24%24%24GBL+0P0000MVDX+0P0000IEQQ+FOUSA099M0+FSAUS08L0Y+348213+0P0000IQG3+F0GBR06GCZ%70L00000138%7CU%24%24%24%24%24GBL+333451+0P0000IU37+327788+0P00003DYK+0P00000NIG+FSUSA081YU%70L00000122+F000002LPT+446351+0P000068ZR+F000001BMP+F0GBR04DZV%70L00000135%7CU%24%24%24%24%24GBL+F000002LHE+F00000H7P6+0P0000IG8F+0P00007T4I+0P00006W5W+0P00001QS3+F0GBR060EK%70L00000138%7CU%24%24%24%24%24GBL+FSUSA04CAR+F0GBR0549Q+FSGBR04GPE%70L00000408+F00000JXCE+240215+F00000048U+531508+FSUSA07XBW+FSUSA07LOV+FOUSA06RCX%70L00000138%7CU%24%24%24%24%24CAN+FSUSA07ROV+F000005KLL+FOUSA00LKM+0P000075PZ+0P00007KFD+FOUSA09BLD+FSUSA09LZ8%70L00000122+0P00000225+FSUSA0A0NM+F000002LHE+0P00007XVN+301470+400069+F00000HDPD+F00000JS55+FSUSA07WEN+F00000MCR9+FSUSA0ANRK+F0GBR054Y0+0P0000ACYE+F000005M0X+FOUSA07ES2+14638+FOUSA06R07+459295+FSGBR06RNK+460158+FSUSA04AKA%70L00000122+FSGBR05AXW%70L00000408+0P0000A63X+F00000J016+0P0000RU6A+FSGBR06M9U%70L00000122+266704+0P0000B47N+0P00001PYZ+F0GBR06W8G%70L00000138%7CU%24%24%24%24%24GBL+490407+FSUSA08HRO%70L00000122+518557+292372+FOUSA05O9G+F0GBR055OV%70L00000138%7CU%24%24%24%24%24GBL+FOUSA097GP+FSGBR06U9E%70L00000152+462878+0P00007Z7U&fq=&start=0&rows=100&fl=PM001+ST735+PM02H+CA516+RR00H+OS38A+HS000+HS04Q+PM00K+HS002+DAS0N+PM02F+DAS29+PM513+DAS0T+HS00C+HS03C+DAS25+PM02G+PM00K+HS004+DAS0P+PM448+PM461+HS08O+HS006+PM00I+PM013+PM455+PM00A+PM140+AA0AQ+RR011+HS04S+ST153+DAS03+PM004+DAS0I+HS08L+HS00V+DAS34+OS020+HS02A+DAS05+RR015+PM142+ST735+RR03A+HS07S+ST735+HS157+DAS30+PM02D+RR01U+AA03N+HS04A+DAS36+PM140+PM528+HS148+PM142+DAS41+OF00H+AA03N+PM522+OS01X+PM133+PM151+PM428+DAS33+PM00I+PM00I+HS05E+PM541+DAS21+OS687+RR01T+PM58B+RR01E+DAS39+PM457+RR00M+HS079+HS02I+DAS35+PM008+PM581+PM00E+PM457+DAS21+HS04C+AA0A8+DAS07+OS384+HS004+RR002+RR00I+PM00O+HS02I+DAS29+PM00V+HS081+HS092+PM00Q+ST735+HS06E+RR00I+PM425+OS05J+PM00Q+DAS37+AA03R+OS050+DAS31+HS02L+HS06H+RR102+HS08O+PM006+DAS0B+HS078+AA03R+OS463+CA514+ST154+DAS15+HS00V+PM426+HS06K+DAS30+DAS0E+DAS22+PM00Q+PM458+PM02D+DAS32+PM481+PM00Y+PM461+PM58B+AA0F3+DAS06+DAS35+OS050+DAS31+RR03A+AA03Q+OS076+HS03I+RR00M&qt=standard&wt=standard&explainOther=&hl.fl=
    */
    HINTERNET hInternet = InternetOpenA("Testing", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); //初始化WinINet
    char strSever[] = "www.baidu.com";
    HINTERNET hConnect = InternetConnectA(hInternet, strSever, INTERNET_DEFAULT_HTTP_PORT,
        NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); //连接服务器
    char strObject[] = "/solr/select";
    HINTERNET hOpenRequest = HttpOpenRequestA(hConnect, "POST", strObject, NULL, NULL,
        (LPCSTR*)"*/*", INTERNET_FLAG_DONT_CACHE, 1); //创建http请求
    char hdrs[] = "Content-Type: application/x-www-form-urlencoded";
    char options[] = "indent=on&version=2.2&q=0P00000UG6+0P0000GLUK+157498+FSGBR04KHF%70L00000122+FSUSA04CAR+0P0000J395+FSUSA08H7B%70L00000138+0P00000F5D+558658+F0CAN062AS%70L00000138%7CU%24%24%24%24%24GBL+FSUSA0BDMH%70L00000204+F0000007T0+0P00000IH1+0P00001KTE+FSUSA0APV9%70L00000122+528765+0P00007XVN+F000000L2B%70L00000138%7CU%24%24%24%24%24GBL+0P0000MVDX+0P0000IEQQ+FOUSA099M0+FSAUS08L0Y+348213+0P0000IQG3+F0GBR06GCZ%70L00000138%7CU%24%24%24%24%24GBL+333451+0P0000IU37+327788+0P00003DYK+0P00000NIG+FSUSA081YU%70L00000122+F000002LPT+446351+0P000068ZR+F000001BMP+F0GBR04DZV%70L00000135%7CU%24%24%24%24%24GBL+F000002LHE+F00000H7P6+0P0000IG8F+0P00007T4I+0P00006W5W+0P00001QS3+F0GBR060EK%70L00000138%7CU%24%24%24%24%24GBL+FSUSA04CAR+F0GBR0549Q+FSGBR04GPE%70L00000408+F00000JXCE+240215+F00000048U+531508+FSUSA07XBW+FSUSA07LOV+FOUSA06RCX%70L00000138%7CU%24%24%24%24%24CAN+FSUSA07ROV+F000005KLL+FOUSA00LKM+0P000075PZ+0P00007KFD+FOUSA09BLD+FSUSA09LZ8%70L00000122+0P00000225+FSUSA0A0NM+F000002LHE+0P00007XVN+301470+400069+F00000HDPD+F00000JS55+FSUSA07WEN+F00000MCR9+FSUSA0ANRK+F0GBR054Y0+0P0000ACYE+F000005M0X+FOUSA07ES2+14638+FOUSA06R07+459295+FSGBR06RNK+460158+FSUSA04AKA%70L00000122+FSGBR05AXW%70L00000408+0P0000A63X+F00000J016+0P0000RU6A+FSGBR06M9U%70L00000122+266704+0P0000B47N+0P00001PYZ+F0GBR06W8G%70L00000138%7CU%24%24%24%24%24GBL+490407+FSUSA08HRO%70L00000122+518557+292372+FOUSA05O9G+F0GBR055OV%70L00000138%7CU%24%24%24%24%24GBL+FOUSA097GP+FSGBR06U9E%70L00000152+462878+0P00007Z7U&fq=&start=0&rows=100&fl=PM001+ST735+PM02H+CA516+RR00H+OS38A+HS000+HS04Q+PM00K+HS002+DAS0N+PM02F+DAS29+PM513+DAS0T+HS00C+HS03C+DAS25+PM02G+PM00K+HS004+DAS0P+PM448+PM461+HS08O+HS006+PM00I+PM013+PM455+PM00A+PM140+AA0AQ+RR011+HS04S+ST153+DAS03+PM004+DAS0I+HS08L+HS00V+DAS34+OS020+HS02A+DAS05+RR015+PM142+ST735+RR03A+HS07S+ST735+HS157+DAS30+PM02D+RR01U+AA03N+HS04A+DAS36+PM140+PM528+HS148+PM142+DAS41+OF00H+AA03N+PM522+OS01X+PM133+PM151+PM428+DAS33+PM00I+PM00I+HS05E+PM541+DAS21+OS687+RR01T+PM58B+RR01E+DAS39+PM457+RR00M+HS079+HS02I+DAS35+PM008+PM581+PM00E+PM457+DAS21+HS04C+AA0A8+DAS07+OS384+HS004+RR002+RR00I+PM00O+HS02I+DAS29+PM00V+HS081+HS092+PM00Q+ST735+HS06E+RR00I+PM425+OS05J+PM00Q+DAS37+AA03R+OS050+DAS31+HS02L+HS06H+RR102+HS08O+PM006+DAS0B+HS078+AA03R+OS463+CA514+ST154+DAS15+HS00V+PM426+HS06K+DAS30+DAS0E+DAS22+PM00Q+PM458+PM02D+DAS32+PM481+PM00Y+PM461+PM58B+AA0F3+DAS06+DAS35+OS050+DAS31+RR03A+AA03Q+OS076+HS03I+RR00M&qt=standard&wt=standard&explainOther=&hl.fl=";
    BOOL bRequest = HttpSendRequestA(hOpenRequest, hdrs, (DWORD)strlen(hdrs), options, (DWORD)strlen(options)); //发送http请求
    char szBuffer[1024] = { 0 };
    DWORD dwByteRead = 0;
    FILE* fp = NULL;
    fopen_s(&fp, "1.xml", "w");
    while (InternetReadFile(hOpenRequest, szBuffer, sizeof(szBuffer), &dwByteRead) && dwByteRead > 0)
    {
        fwrite(szBuffer, dwByteRead, 1, fp);
        ZeroMemory(szBuffer, dwByteRead);
    }
    fclose(fp);
    return 0;
}
没搞明白我要发送的数据是在哪个函数里体现的?发送到指定地址怎么修改?怎样判断是否发送成功?

加载中
返回顶部
顶部