zxk123 发表于 2011-10-25 09:02:20

如何实现后台发送网址然后获取返回网址?

有没有办法在后台不使用浏览器情况下给服务器发送网址,然后获取返回的网址?
第一次发问,不知道这能不能实现?{:face (245):}

happytc 发表于 2011-10-25 09:20:47

回复 1# zxk123


    用TCP或UDP之类的,但前提是服务器会接收你的数据包,并且返回信息。

zxk123 发表于 2011-10-25 09:27:24

本帖最后由 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;
      unsigned long lNumberOfBytesRead;
      bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);

//an>
      if(!bRet || !lNumberOfBytesRead)
            break;
      cReadBuffer = 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 = {0};
char cReadBuffer;
    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 = 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 = {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的看不懂,求大神分析下。

devil3380 发表于 2011-10-25 09:40:01

上面是C++的嘛

happytc 发表于 2011-10-25 10:16:18

本帖最后由 happytc 于 2011-10-25 10:32 编辑

回复 3# zxk123

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

你是这个意思呀,参见‘获取网络文件修改时间’的6楼例子
http://www.autoitx.com/forum.php?mod=viewthread&tid=28036&highlight=%CD%F8%C2%E7%CE%C4%BC%FE

另外参见republican的_WinHTTP_GetRespond
http://www.autoitx.com/thread-18528-1-1.html

asdasdasd 发表于 2011-11-12 23:38:21

可以在后台下载找开然后再获取的,仔细看下帮助文件!
页: [1]
查看完整版本: 如何实现后台发送网址然后获取返回网址?