【发布时间】:2016-09-13 17:45:14
【问题描述】:
我正在尝试编写一个从远程服务器下载一些东西的程序,
#include <iostream>
#include <string>
#include <Windows.h>
#include <WinInet.h>
#pragma comment(lib,"wininet.lib")
using namespace std;
string Get(){
DWORD size = 0;
DWORD wrt;
string msg = "";
HINTERNET io=InternetOpen("Downloader",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
HINTERNET ic=InternetConnect(io,"192.168.1.15",8080,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
HINTERNET hreq=HttpOpenRequest(ic,NULL,"/cgi-bin/cmd.py","HTTP/1.0",NULL,NULL,0,0);
HttpSendRequest(hreq,NULL,0,NULL,0);
InternetQueryDataAvailable(hreq,&size,0,0);
char* buffer = new char[size+1];
memset(buffer,0,size+1);
InternetReadFile(hreq,buffer,size,&wrt);
msg += buffer;
free(buffer);
InternetCloseHandle(io);
InternetCloseHandle(ic);
InternetCloseHandle(hreq);
return msg;
}
int main(){
while(TRUE){
string msg=Get();
if(msg.length()>1){
cout<<msg<<endl;
}
Sleep(2000);
}
return 0;
}
在另一端(在服务器上)我运行一个 python CGI 脚本来发送文本。 问题是程序只发送一次 GET 请求,即使有一个循环并且 msg.length() 等于 0 ,在另一边我可以看到我刚刚收到一个 GET 请求。 有人可以解决我的问题,或者任何想法....
【问题讨论】:
-
在调试器下运行应用程序可以收集到哪些见解?
-
您显示的代码是您正在运行的实际代码吗?如果不是,那么在您的实际情况中,您确实会检查您调用的所有函数是否有错误?其中许多可能会失败,而您似乎并未对此进行检查(如果您显示的代码是您的实际代码)。
-
我还没有尝试调试器
-
@TarekRadah 始终在此处发布之前进行调试。
-
char* buffer = new char[size+1];后跟free(buffer)是错误的。new和delete,或者malloc和free。
标签: c++ http winapi wininet winhttp