C++;Wininet自定义http头 < >我使用DEV C++,Wininet LIB从Web下载文件。我正在尝试更改引用者或用户代理。我使用这段代码,它下载成功,但我不知道如何更改http头。谢谢 #include <Windows.h> #include <Wininet.h> #include <iostream> #include <fstream> namespace { ::HINTERNET netstart () { const ::HINTERNET handle = ::InternetOpenW(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0); if ( handle == 0 ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetOpen(): " << error << "." << std::endl; } return (handle); } void netclose ( ::HINTERNET object ) { const ::BOOL result = ::InternetCloseHandle(object); if ( result == FALSE ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetClose(): " << error << "." << std::endl; } } ::HINTERNET netopen ( ::HINTERNET session, ::LPCWSTR url ) { const ::HINTERNET handle = ::InternetOpenUrlW(session, url, 0, 0, 0, 0); if ( handle == 0 ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetOpenUrl(): " << error << "." << std::endl; } return (handle); } void netfetch ( ::HINTERNET istream, std::ostream& ostream ) { static const ::DWORD SIZE = 1024; ::DWORD error = ERROR_SUCCESS; ::BYTE data[SIZE]; ::DWORD size = 0; do { ::BOOL result = ::InternetReadFile(istream, data, SIZE, &size); if ( result == FALSE ) { error = ::GetLastError(); std::cerr << "InternetReadFile(): " << error << "." << std::endl; } ostream.write((const char*)data, size); } while ((error == ERROR_SUCCESS) && (size > 0)); } } int main ( int, char ** ) { const ::WCHAR URL[] = L"http://google.com"; const ::HINTERNET session = ::netstart(); if ( session != 0 ) { const ::HINTERNET istream = ::netopen(session, URL); if ( istream != 0 ) { std::ofstream ostream("googleindex.html", std::ios::binary); if ( ostream.is_open() ) { ::netfetch(istream, ostream); } else { std::cerr << "Could not open 'googleindex.html'." << std::endl; } ::netclose(istream); } ::netclose(session); } } #pragma comment ( lib, "Wininet.lib" ) #包括 #包括 #包括 #包括 名称空间{ ::HINTERNET netstart() { const::HINTERNET句柄= ::互联网开放(0,互联网开放式,直接,0,0,0); 如果(句柄==0) { 常量::DWORD错误=::GetLastError(); 标准:cerr

C++;Wininet自定义http头 < >我使用DEV C++,Wininet LIB从Web下载文件。我正在尝试更改引用者或用户代理。我使用这段代码,它下载成功,但我不知道如何更改http头。谢谢 #include <Windows.h> #include <Wininet.h> #include <iostream> #include <fstream> namespace { ::HINTERNET netstart () { const ::HINTERNET handle = ::InternetOpenW(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0); if ( handle == 0 ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetOpen(): " << error << "." << std::endl; } return (handle); } void netclose ( ::HINTERNET object ) { const ::BOOL result = ::InternetCloseHandle(object); if ( result == FALSE ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetClose(): " << error << "." << std::endl; } } ::HINTERNET netopen ( ::HINTERNET session, ::LPCWSTR url ) { const ::HINTERNET handle = ::InternetOpenUrlW(session, url, 0, 0, 0, 0); if ( handle == 0 ) { const ::DWORD error = ::GetLastError(); std::cerr << "InternetOpenUrl(): " << error << "." << std::endl; } return (handle); } void netfetch ( ::HINTERNET istream, std::ostream& ostream ) { static const ::DWORD SIZE = 1024; ::DWORD error = ERROR_SUCCESS; ::BYTE data[SIZE]; ::DWORD size = 0; do { ::BOOL result = ::InternetReadFile(istream, data, SIZE, &size); if ( result == FALSE ) { error = ::GetLastError(); std::cerr << "InternetReadFile(): " << error << "." << std::endl; } ostream.write((const char*)data, size); } while ((error == ERROR_SUCCESS) && (size > 0)); } } int main ( int, char ** ) { const ::WCHAR URL[] = L"http://google.com"; const ::HINTERNET session = ::netstart(); if ( session != 0 ) { const ::HINTERNET istream = ::netopen(session, URL); if ( istream != 0 ) { std::ofstream ostream("googleindex.html", std::ios::binary); if ( ostream.is_open() ) { ::netfetch(istream, ostream); } else { std::cerr << "Could not open 'googleindex.html'." << std::endl; } ::netclose(istream); } ::netclose(session); } } #pragma comment ( lib, "Wininet.lib" ) #包括 #包括 #包括 #包括 名称空间{ ::HINTERNET netstart() { const::HINTERNET句柄= ::互联网开放(0,互联网开放式,直接,0,0,0); 如果(句柄==0) { 常量::DWORD错误=::GetLastError(); 标准:cerr,c++,http,header,wininet,C++,Http,Header,Wininet,将用户代理字符串作为第一个参数传递给InternetOpen 使用HttpOpenRequest和HttpSendRequest代替InternetOpenUrl。Referer字符串是HttpOpenRequest的第五个参数,第三个参数是lpszHeaders[in](来自MSDN): 指向以null结尾的字符串的指针,该字符串指定要发送到HTTP服务器的标头。有关详细信息,请参阅HttpSendRequest函数中lpszHeaders参数的说明 您可以这样设置Referer和User a

将用户代理字符串作为第一个参数传递给
InternetOpen

使用
HttpOpenRequest
HttpSendRequest
代替
InternetOpenUrl
。Referer字符串是
HttpOpenRequest
的第五个参数,第三个参数是lpszHeaders[in](来自MSDN):

指向以null结尾的字符串的指针,该字符串指定要发送到HTTP服务器的标头。有关详细信息,请参阅HttpSendRequest函数中lpszHeaders参数的说明

您可以这样设置Referer和User agent:

LPWSTR headers = L"User-Agent: myagent\r\nReferer: my.referer.com\r\n\r\n\r\n";
//and then call
::InternetOpenUrlW(session, url, headers, -1, 0, 0);

您必须用分隔每个标头\r\n,并用WinInet文档中“HTTP会话”中提到的\r\n\r\n关闭块。值得注意的是InternetOpenUrl和HttpSendRequest都有headers参数