WinInet编程(学习)
来源:互联网 发布:1 10月进出口数据 编辑:程序博客网 时间:2024/06/03 20:25
客户端从HTTP服务器下载相应的MP3文件的核心代码(HTTP中的GET方法):#include <Windows.h>#include <wininet.h>#pragma comment(lib, "wininet.lib")#define URL_STRING_TEST "http://eng.edu-edu.com.cn/audio/Onelove.mp3"void main(){ URL_COMPONENTS crackedURL; TCHAR szBuffer[1024];//这里是下载缓冲区大小 1KB大小缓冲写入一次 TCHAR szHostName[128]; TCHAR szUrlPath[256]; ZeroMemory(&crackedURL, sizeof (URL_COMPONENTS)); crackedURL.dwStructSize = sizeof (URL_COMPONENTS); crackedURL.lpszHostName = szHostName; crackedURL.dwHostNameLength = sizeof(szHostName); crackedURL.lpszUrlPath = szUrlPath; crackedURL.dwUrlPathLength = sizeof(szUrlPath); InternetCrackUrl(URL_STRING_TEST,(DWORD)strlen(URL_STRING_TEST),0,&crackedURL); FILE* file = fopen("Onelove.mp3", "wb"); HINTERNET hInt,hConn,hReq; //启用HTTP协议 hInt = InternetOpen("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); //建立HTTP连接 hConn = InternetConnect(hInt,crackedURL.lpszHostName,crackedURL.nPort,NULL,NULL,INTERNET_SERVICE_HTTP,0,0); //创建一个URL请求 hReq = HttpOpenRequest(hConn, "GET", crackedURL.lpszUrlPath, NULL, "", NULL, 0, 0); char buff[64]; DWORD dwContLen = 0; DWORD dwLen; BOOL bResult = FALSE; DWORD nBytesGet = 0; BOOL bEnd = FALSE; DWORD dwRetCode = 0; DWORD dwSizeOfRq = sizeof(DWORD); dwSizeOfRq = sizeof(buff); if (HttpSendRequest(hReq,NULL,0,NULL,0) && HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL) && dwRetCode < 400) { bResult = TRUE;//true; } //查询文件大小 if (HttpQueryInfo(hReq, HTTP_QUERY_CONTENT_LENGTH, &buff, &dwSizeOfRq, NULL)) dwContLen = atol(buff); INTERNET_BUFFERS BufferIn; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = strHeader.c_str(); // 请求头 BufferIn.dwHeadersLength = strHeader.length(); BufferIn.dwHeadersTotal = 0; BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = 1024; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; */ hReq = HttpOpenRequest(hConn, "GET", crackedURL.lpszUrlPath, NULL, "", NULL, 0, 0); if (HttpSendRequest(hReq,NULL,0,NULL,0) && HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL) && dwRetCode < 400) { bResult = TRUE;//true; } bool bTrue = HttpSendRequestExA(hReq,&BufferIn,NULL,NULL,0); while(TRUE) { if (bResult) { //开始读取文件 bResult = InternetReadFile(hReq, szBuffer, sizeof(szBuffer), &dwLen); if (bResult) { cout<<"reading ... "<<(nBytesGet*100/dwContLen)<<endl; nBytesGet += dwLen; if (dwLen == 0) { bEnd = TRUE; break; } fwrite(szBuffer, 1, dwLen, file); } } else //数据接受完毕 { break; } } fclose(file);}===================================================POST方法上传数据(大量)#include #include #include BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize);#define BUFFSIZE 500void main( int argc, char **argv ){ DWORD dwPostSize; if (argc < 4) { printf("Usage: Bigpost \n"); printf(" is the number of KB to POST\n"); printf(" is the server to POST to\n"); printf(" is the virtual path to POST to\n"); exit(0); } if ( ((dwPostSize = strtoul(argv[1],NULL,10)) == 0) || (dwPostSize >=) ) { printf("%s is invalid size. Valid sizes are from 1 to 2047999\n",[1]); exit(0); } printf( "Test of POSTing %luKB with WinInet\n", dwPostSize); dwPostSize *= 1024; // Convert KB to bytes HINTERNET hSession = InternetOpen( "HttpSendRequestEx",, NULL, NULL, 0); if(!hSession) { printf("Failed to open session\n"); exit(0); } HINTERNET hConnect = InternetConnect(hSession, argv[2],, NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL); if (!hConnect) printf( "Failed to connect\n" ); else { HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", argv[3], NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); if (!hRequest) printf( "Failed to open request handle\n" ); else { if(UseHttpSendReqEx(hRequest, dwPostSize)) { char pcBuffer[BUFFSIZE]; DWORD dwBytesRead; printf("\nThe following was returned by the server:\n"); do { dwBytesRead=0; if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1,&dwBytesRead)) { pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer printf("%s", pcBuffer); } else printf("\nInternetReadFile failed"); }while(dwBytesRead>0); printf("\n"); } if (!InternetCloseHandle(hRequest)) printf( "Failed to close Request handle\n" ); } if(!InternetCloseHandle(hConnect)) printf("Failed to close Connect handle\n"); } if( InternetCloseHandle( hSession ) == FALSE ) printf( "Failed to close Session handle\n" ); printf( "\nFinished.\n" );}BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize){ INTERNET_BUFFERS BufferIn; DWORD dwBytesWritten; int n; BYTE pBuffer[1024]; BOOL bRet; BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = NULL; BufferIn.dwHeadersLength = 0; BufferIn.dwHeadersTotal = 0; BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0)) { printf( "Error on HttpSendRequestEx %d\n",GetLastError() ); return FALSE; } FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data bRet=TRUE; for(n=1; n<=(int)dwPostSize/1024 && bRet; n++) { if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten)) printf( "\r%d bytes sent.", n*1024); } if(!bRet) { printf( "\nError on InternetWriteFile %lu\n",GetLastError() ); return FALSE; } if(!HttpEndRequest(hRequest, NULL, 0, 0)) { printf( "Error on HttpEndRequest %lu \n", GetLastError()); return FALSE; } return TRUE;}====================================================HTTP服务器:一般都是socket异步多线程(解析相应的HTTP协议,然后按照用户的命令(GET,POST,CGI))进行相应的处理LAMP(Linux,apache,mysql,php)http://baike.baidu.com/view/365086.htm?fr=ala0_1_1
0 0
- WinInet编程(学习)
- winInet编程
- WinInet编程
- WinInet编程
- 学习WinInet
- WinInet编程经验点滴
- WinInet 网络编程
- WinInet 编程 参考
- WinInet编程详解
- WinInet编程详解
- WinInet编程详解 (好)
- 简单的WinInet编程
- WinInet编程详解
- WinINet 学习资料
- WinINet 学习资料
- 网络编程--WININET
- 网络编程--WININET
- 网络编程--WININET
- mysql创建定时器(event),查看定时器,打开定时器,设置定时器时间
- button实现href
- Sqlite-JDBC查询datetime类型抛异常
- Robot Framework中经常用的第三方库的安装方法
- Spring AOP
- WinInet编程(学习)
- Struts 不同namespace之间的action的跳转
- Codeforces 451E Devu and Flowers 容斥原理暴力+Lucas定理
- Kinect 骨骼追踪数据的处理方法
- unity实现按钮和攻击对象的选定
- Makefile学习笔记<文件名操作函数>
- Android进阶之路
- 文章标题
- linux 下的v4l2运行