找回密码
 加入
搜索
查看: 5321|回复: 5

[IE类操作] 如何实现后台发送网址然后获取返回网址?

  [复制链接]
发表于 2011-10-25 09:02:20 | 显示全部楼层 |阅读模式
有没有办法在后台不使用浏览器情况下给服务器发送网址,然后获取返回的网址?
第一次发问,不知道这能不能实现?
发表于 2011-10-25 09:20:47 | 显示全部楼层
回复 1# zxk123


    用TCP或UDP之类的,但前提是服务器会接收你的数据包,并且返回信息。
 楼主| 发表于 2011-10-25 09:27:24 | 显示全部楼层
本帖最后由 zxk123 于 2011-10-25 09:28 编辑
  1. 控制台程序main.cpp如下(这个例子是参考来的,一些地方进行了修改)

  2. #define API_VERSION
  3. #define BUF_SIZE 4096

  4. #include <afxwin.h>
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include "Wininet.h"

  8. #pragma comment(lib,"Wininet.lib")

  9. //模拟浏览器发送HTTP请求函数
  10. #ifndef API_VERSION
  11. CString HttpRequest(TCHAR * lpHostName,short sPort,TCHAR * lpUrl,TCHAR * lpMethod,TCHAR * lpPostData,unsigned int nPostDataLen)
  12. {
  13.     HINTERNET hInternet,hConnect,hRequest;
  14.     BOOL bRet;CString strResponse;

  15.     hInternet = InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  16.     if(!hInternet)
  17.         goto Ret0;

  18.     hConnect = InternetConnect(hInternet,lpHostName,sPort,NULL,_T("HTTP/1.1"),INTERNET_SERVICE_HTTP,0,0);
  19.     if(!hConnect)
  20.         goto Ret0;

  21.     hRequest = HttpOpenRequest(hConnect,lpMethod,lpUrl,_T("HTTP/1.1"),NULL,NULL,INTERNET_FLAG_RELOAD,0);
  22.     if(!hRequest)
  23.         goto Ret0;

  24.     //bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
  25.     //if(!bRet)
  26.         //goto Ret0;

  27.     bRet = HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);

  28.     while(TRUE)
  29.     {
  30.         char cReadBuffer[BUF_SIZE];
  31.         unsigned long lNumberOfBytesRead;
  32.         bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);

  33. //an>
  34.         if(!bRet || !lNumberOfBytesRead)
  35.             break;
  36.         cReadBuffer[lNumberOfBytesRead] = 0;
  37.         strResponse = strResponse + cReadBuffer;
  38.     }

  39. Ret0:
  40.     if(hRequest)
  41.         InternetCloseHandle(hRequest);
  42.     if(hConnect)
  43.         InternetCloseHandle(hConnect);
  44.     if(hInternet)
  45.         InternetCloseHandle(hInternet);

  46.     return strResponse;
  47. }

  48. #include   <afxpriv.h>   

  49. void main()
  50. {
  51.     // 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")));
  52.    CString strResponse = HttpRequest(_T("localhost"),80,_T("/cake/test1.php?str=中文张三&num=3"),_T("GET"),NULL,0);
  53.     FILE * fp = NULL;
  54. _wfopen_s(&fp, _T("C:\\123.htm"),_T("wb"));
  55. USES_CONVERSION;
  56.     fwrite(T2A(strResponse),strResponse.GetLength(),1,fp);
  57.     fclose(fp);
  58.     ::MessageBox(NULL,strResponse,_T("123"),0);
  59. }

  60. #else
  61. int HttpRequest(char * lpOutBuffer, TCHAR * lpHostName,short sPort,TCHAR * lpUrl,TCHAR * lpMethod,TCHAR * lpPostData,unsigned int nPostDataLen)
  62. {
  63.     HINTERNET hInternet,hConnect,hRequest;
  64.     BOOL bRet;
  65. int iResult = 0;
  66. char cResponse[BUF_SIZE] = {0};
  67. char cReadBuffer[BUF_SIZE];
  68.     unsigned long lNumberOfBytesRead;
  69. //打开环境
  70.     hInternet = InternetOpen(_T("User-Agent"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  71. if(!hInternet){
  72.   iResult = 1;
  73.         goto Ret0;
  74. }
  75. //连接
  76.     hConnect = InternetConnect(hInternet,lpHostName,sPort,NULL,_T("HTTP/1.1"),INTERNET_SERVICE_HTTP,0,0);
  77. if(!hConnect){
  78.   iResult = 2;
  79.         goto Ret0;
  80. }
  81. //打开请求句柄
  82.     hRequest = HttpOpenRequest(hConnect,lpMethod,lpUrl,_T("HTTP/1.1"),NULL,NULL,INTERNET_FLAG_RELOAD,0);
  83. if(!hRequest){
  84.   iResult = 3;
  85.         goto Ret0;
  86. }
  87. //添加请求头部
  88. //bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
  89. //if(!bRet)
  90. //goto Ret0;
  91. //发送HTTP请求
  92.     bRet = HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);
  93. //读取顺序字节流
  94.     while(TRUE){
  95.         bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
  96.   if(bRet){  //读取成功
  97.    if(lNumberOfBytesRead == 0){  //已经读到流尾部了
  98.     break;
  99.    }else{
  100.     cReadBuffer[lNumberOfBytesRead] = 0;
  101.     strcat_s(cResponse, cReadBuffer);
  102.    }
  103.   }else{
  104.    iResult = 10;
  105.    break;
  106.   }
  107.     }
  108. //关闭打开的句柄
  109. Ret0:
  110.     if(hRequest)
  111.         InternetCloseHandle(hRequest);
  112.     if(hConnect)
  113.         InternetCloseHandle(hConnect);
  114.     if(hInternet)
  115.         InternetCloseHandle(hInternet);
  116. //读取的数据拷贝到输出缓冲区
  117. strcpy_s(lpOutBuffer, BUF_SIZE - 1, cResponse);  //输出缓冲区大小不得小于BUF_SIZE!!!
  118.     return iResult;
  119. }

  120. void main()
  121. {
  122. char cResponse[BUF_SIZE] = {0};
  123. int iResult;
  124. iResult = HttpRequest(cResponse, _T("localhost"), 80, _T("/cake/test1.php?str=产品葡萄,2009年6月&num=1"), _T("GET"), NULL, 0);
  125.     FILE * fp = NULL;
  126. _wfopen_s(&fp, _T("C:\\123.htm"),_T("wb"));
  127.     fwrite(cResponse, strlen(cResponse), 1, fp);
  128.     fclose(fp);
  129.     ::MessageBoxA(NULL, cResponse, "123", 0);
  130. }
  131. #endif

  132. 在本地服务器localhost启动apache服务器,在对应的文档目录下放上test1.php文件,此服务器文件如下:

  133. <html>
  134. <head></head>
  135. <body>
  136. <?php
  137. echo 'strlen(str) * num = ' . strlen($_GET['str']) * $_GET['num'];
  138. ?>
  139. </body>
  140. </html>

复制代码
这段代码是网上找到的,不是AU3的看不懂,求大神分析下。
发表于 2011-10-25 09:40:01 | 显示全部楼层
上面是C++的嘛
发表于 2011-10-25 10:16:18 | 显示全部楼层
本帖最后由 happytc 于 2011-10-25 10:32 编辑

回复 3# zxk123

你在那里拷的乱78糟的cpp文件。

你是这个意思呀,参见‘获取网络文件修改时间’的6楼例子
http://www.autoitx.com/forum.php ... 8%C2%E7%CE%C4%BC%FE

另外参见republican的_WinHTTP_GetRespond
http://www.autoitx.com/thread-18528-1-1.html
发表于 2011-11-12 23:38:21 | 显示全部楼层
可以在后台下载找开然后再获取的,仔细看下帮助文件!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2026-9-22 16:45 , Processed in 0.062749 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表