wininet实现http协议,GET 方法

  • 时间:
  • 来源:互联网
  • 文章标签:

#include <Windows.h>
#include <Wininet.h>
#include <stdio.h>
#pragma comment(lib, "WinInet.lib")
int main()
{
    // 建立会话
    HINTERNET hInternet;
    hInternet = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if(hInternet == NULL) return false;
    // 建立连接
    HINTERNET hConnect;
    hConnect = InternetConnect(hInternet, "jwc.xidian.edu.cn", INTERNET_INVALID_PORT_NUMBER,"", "", INTERNET_SERVICE_HTTP, INTERNET_FLAG_PASSIVE, 0);
    if(hInternet == NULL) { InternetCloseHandle(hInternet); return false; }
    HINTERNET httpFile;
   httpFile = HttpOpenRequest(hConnect, "GET", "2j-ce-nry.jsp?urltype=news.NewsContentUrl&wbtreeid=1012&wbnewsid=3118", HTTP_VERSION, NULL, 0, INTERNET_FLAG_NO_UI | INTERNET_FLAG_DONT_CACHE, 1);
    if(httpFile == NULL) 
{   
InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return false; 
}
    HttpSendRequest(httpFile, NULL, NULL, 0, 0);
    char buf[1024];
    DWORD buf_len = 1024;
    DWORD buf_read = 1024;
    FILE *fp = fopen("d:\\test.html", "wb");
    while(1)
    {
       InternetReadFile(httpFile, buf, buf_len, &buf_read);
       if(buf_read == 0) break;
       fwrite(buf, 1, buf_read, fp);
    }
    fclose(fp);
    InternetCloseHandle(httpFile);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);
    return true;
}

本文链接http://www.taodudu.cc/news/show-647735.html