本帖最后由 zxk123 于 2011-10-25 09:28 编辑
- 控制台程序main.cpp如下(这个例子是参考来的,一些地方进行了修改)
- #define API_VERSION
- #define BUF_SIZE 4096
- #include <afxwin.h>
- #include <stdio.h>
- #include <windows.h>
- #include "Wininet.h"
- #pragma comment(lib,"Wininet.lib")
- //模拟浏览器发送HTTP请求函数
- #ifndef API_VERSION
- CString HttpRequest(TCHAR * lpHostName,short sPort,TCHAR * lpUrl,TCHAR * lpMethod,TCHAR * lpPostData,unsigned int nPostDataLen)
- {
- HINTERNET hInternet,hConnect,hRequest;
- BOOL bRet;CString strResponse;
- hInternet = InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
- if(!hInternet)
- goto Ret0;
- hConnect = InternetConnect(hInternet,lpHostName,sPort,NULL,_T("HTTP/1.1"),INTERNET_SERVICE_HTTP,0,0);
- if(!hConnect)
- goto Ret0;
- hRequest = HttpOpenRequest(hConnect,lpMethod,lpUrl,_T("HTTP/1.1"),NULL,NULL,INTERNET_FLAG_RELOAD,0);
- if(!hRequest)
- goto Ret0;
- //bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
- //if(!bRet)
- //goto Ret0;
- bRet = HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);
- while(TRUE)
- {
- char cReadBuffer[BUF_SIZE];
- unsigned long lNumberOfBytesRead;
- bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
- //an>
- if(!bRet || !lNumberOfBytesRead)
- break;
- cReadBuffer[lNumberOfBytesRead] = 0;
- strResponse = strResponse + cReadBuffer;
- }
- Ret0:
- if(hRequest)
- InternetCloseHandle(hRequest);
- if(hConnect)
- InternetCloseHandle(hConnect);
- if(hInternet)
- InternetCloseHandle(hInternet);
- return strResponse;
- }
- #include <afxpriv.h>
- void main()
- {
- // CString strResponse = HttpRequest(_T("translate.google.com"),80,_T("/translate_t?langpair=en|zh-CN"),_T("POST"),_T("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN"),wcslen(_T("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN")));
- CString strResponse = HttpRequest(_T("localhost"),80,_T("/cake/test1.php?str=中文张三&num=3"),_T("GET"),NULL,0);
- FILE * fp = NULL;
- _wfopen_s(&fp, _T("C:\\123.htm"),_T("wb"));
- USES_CONVERSION;
- fwrite(T2A(strResponse),strResponse.GetLength(),1,fp);
- fclose(fp);
- ::MessageBox(NULL,strResponse,_T("123"),0);
- }
- #else
- int HttpRequest(char * lpOutBuffer, TCHAR * lpHostName,short sPort,TCHAR * lpUrl,TCHAR * lpMethod,TCHAR * lpPostData,unsigned int nPostDataLen)
- {
- HINTERNET hInternet,hConnect,hRequest;
- BOOL bRet;
- int iResult = 0;
- char cResponse[BUF_SIZE] = {0};
- char cReadBuffer[BUF_SIZE];
- unsigned long lNumberOfBytesRead;
- //打开环境
- hInternet = InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
- if(!hInternet){
- iResult = 1;
- goto Ret0;
- }
- //连接
- hConnect = InternetConnect(hInternet,lpHostName,sPort,NULL,_T("HTTP/1.1"),INTERNET_SERVICE_HTTP,0,0);
- if(!hConnect){
- iResult = 2;
- goto Ret0;
- }
- //打开请求句柄
- hRequest = HttpOpenRequest(hConnect,lpMethod,lpUrl,_T("HTTP/1.1"),NULL,NULL,INTERNET_FLAG_RELOAD,0);
- if(!hRequest){
- iResult = 3;
- goto Ret0;
- }
- //添加请求头部
- //bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
- //if(!bRet)
- //goto Ret0;
- //发送HTTP请求
- bRet = HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);
- //读取顺序字节流
- while(TRUE){
- bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
- if(bRet){ //读取成功
- if(lNumberOfBytesRead == 0){ //已经读到流尾部了
- break;
- }else{
- cReadBuffer[lNumberOfBytesRead] = 0;
- strcat_s(cResponse, cReadBuffer);
- }
- }else{
- iResult = 10;
- break;
- }
- }
- //关闭打开的句柄
- Ret0:
- if(hRequest)
- InternetCloseHandle(hRequest);
- if(hConnect)
- InternetCloseHandle(hConnect);
- if(hInternet)
- InternetCloseHandle(hInternet);
- //读取的数据拷贝到输出缓冲区
- strcpy_s(lpOutBuffer, BUF_SIZE - 1, cResponse); //输出缓冲区大小不得小于BUF_SIZE!!!
- return iResult;
- }
- void main()
- {
- char cResponse[BUF_SIZE] = {0};
- int iResult;
- iResult = HttpRequest(cResponse, _T("localhost"), 80, _T("/cake/test1.php?str=产品葡萄,2009年6月&num=1"), _T("GET"), NULL, 0);
- FILE * fp = NULL;
- _wfopen_s(&fp, _T("C:\\123.htm"),_T("wb"));
- fwrite(cResponse, strlen(cResponse), 1, fp);
- fclose(fp);
- ::MessageBoxA(NULL, cResponse, "123", 0);
- }
- #endif
- 在本地服务器localhost启动apache服务器,在对应的文档目录下放上test1.php文件,此服务器文件如下:
- <html>
- <head></head>
- <body>
- <?php
- echo 'strlen(str) * num = ' . strlen($_GET['str']) * $_GET['num'];
- ?>
- </body>
- </html>
复制代码 这段代码是网上找到的,不是AU3的看不懂,求大神分析下。 |