Win32 中是否没有用于本机 C/C++ 的“高级”HTTP 库,或者我只是找错了地方?
我所说的“高级”是指一个 API,它允许我使用与 .NET 框架“大致相同”的抽象级别在 C++ 中执行 HTTP Web 请求/响应(但请注意,使用 C++/CLI 不是我的选择)。
如何在 Win32 中的 C/C++ 中执行类似的操作(使用大约相同数量的代码)而不使用 .NET?作为参考,我提供了一个代码示例来展示如何在 C# 中执行此操作。
byte[] fileBytes = null;
bool successfulDownload = false;
using (WebClient client = new WebClient())
{
WebProxy proxy = WebProxy.GetDefaultProxy();
client.Proxy = proxy;
tryAgain:
try
{
fileBytes = client.DownloadData(fileUrl);
successfulDownload = true;
}
catch (WebException wEx)
{
if (wEx.Response != null && wEx.Response is HttpWebResponse)
{
string username = null, password = null;
bool userCanceled = false;
HttpStatusCode statusCode = ((HttpWebResponse)wEx.Response).StatusCode;
switch (statusCode)
{
case HttpStatusCode.ProxyAuthenticationRequired:
// This is just a convenience function defined elsewhere
GetAuthenticationCredentials(fileUrl, true,
out username, out password, out userCanceled);
if (!userCanceled)
{
client.Proxy.Credentials = new NetworkCredential(username, password);
goto tryAgain;
}
break;
case HttpStatusCode.Unauthorized:
// This is just a convenience function defined elsewhere
GetAuthenticationCredentials(fileUrl, false,
out username, out password, out userCanceled);
if (!userCanceled)
{
client.Credentials = new NetworkCredential(username, password);
goto tryAgain;
}
break;
}
}
}
}
Win32 提供了Internet*
功能。
http://msdn.microsoft.com/en-us/library/aa385473(VS.85).aspx
你需要做一个(IIRC,我已经 10 多年没有接触过这些 API)InternetOpenURL
and InternetReadFile
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)