如何从用 MFC (MSVC 2008) 编写的应用程序查询基于 CGI 的网络服务器并处理结果?
Posted
技术标签:
【中文标题】如何从用 MFC (MSVC 2008) 编写的应用程序查询基于 CGI 的网络服务器并处理结果?【英文标题】:How to query a CGI based webserver from an app written in MFC (MSVC 2008) and process the result? 【发布时间】:2010-05-20 14:15:09 【问题描述】:我正在探索使用搜索字符串(比如http://sw.mycompany.com/~tools/cgi-bin/script.cgi?param1=value1¶m2=value2¶m3=value3 的形式)查询网页的选项,该网页的末尾运行了一个 CGI 脚本,并在我的应用程序上显示结果(到期后当然是处理)。我的应用程序是用 MFC C++ 编写的,我必须承认我以前从未尝试过任何与网络编程相关的事情。
我想做的事情是不是很不可行?如果没有,谁能指出我需要查看的资源才能做到这一点?
谢谢!
【问题讨论】:
【参考方案1】:CInternetSession internet_session;
CHttpConnection* connection = NULL;
CHttpFile* file = NULL;
TRY
connection = internet_session.GetHttpConnection("www.somehost.com", (INTERNET_PORT)80);
// URI needs to be a relative path here
file = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, "/some/file/on/server", NULL, 1, NULL, "HTTP/1.1");
BOOL send_result = file->SendRequest();
if (send_result == FALSE)
// When can this happen? Only when connection fails between this and the previous call.
// Need to use ugly MFC exception stuff because OpenRequest et al. use it.
::AfxThrowInternetException(internet_session.GetContext());
CATCH (CInternetException, e)
if (file)
file->Close();
delete connection;
file = NULL;
connection = NULL;
END_CATCH
if (!file)
delete file;
delete connection;
return false;
DWORD status_code;
file->QueryInfoStatusCode(status_code);
if (status_code != 200)
CString result;
if (status_code == 403)
result.Format("Authentication error (HTTP 403)");
else if (status_code == 404)
result.Format("Object not found (HTTP 404)");
else if (status_code == 500)
result.Format("Application error: malformed request (HTTP 500)");
else
result.Format("Got unsupported HTTP status code %d", status_code);
file->Close();
delete file;
delete connection;
return false;
【讨论】:
【参考方案2】:MFC 支持构建 Http 客户端。有关详细信息,请参阅 MSDN 中的 article。
这个question 也可能有用。
【讨论】:
以上是关于如何从用 MFC (MSVC 2008) 编写的应用程序查询基于 CGI 的网络服务器并处理结果?的主要内容,如果未能解决你的问题,请参考以下文章