📄 w3c.cpp
字号:
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_FORMS_SUBMIT |
(_t==w3https? INTERNET_FLAG_SECURE:0),
NULL); // context call-back point
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
// REPLACE HEADER
if(!::HttpAddRequestHeaders( _hRequest, __HTTP_ACCEPT, strlen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE))
throw "additional header failed...";
// COOKIE ADD
if(_listcookies.size()>0){
string szurl;
switch(_t) {
case w3http:
szurl="http://";
break;
case w3https:
szurl="https://";
break;
}
szurl+=_szaddress.c_str();
if(!((_t==w3http && _nport==INTERNET_DEFAULT_HTTP_PORT) || (_t==w3https && _nport==INTERNET_DEFAULT_HTTPS_PORT))){
char tmp[10]="\0";
sprintf(tmp, ":%d", _nport);
szurl+=tmp;
}
szurl+=szuri;
for(list<HTTP_COOKIE*>::iterator it=_listcookies.begin(); it!=_listcookies.end(); it++){
HTTP_COOKIE *pc=reinterpret_cast<HTTP_COOKIE*>(*it);
if(!::InternetSetCookie(szurl.c_str(), pc->name.c_str(), pc->value.c_str()))
throw "add cookie failed...";
}
}
// GET POST ARGUMENTS
buf=reinterpret_cast<unsigned char*>(::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, GetPostArgumentsLength()+1));
len=GetPostData(buf, GetPostArgumentsLength()+1);
// SEND REQUEST WITH POST ARGUEMENTS
if(!::HttpSendRequest( _hRequest, // handle by returned HttpOpenRequest
szContentType, // additional HTTP header
strlen(szContentType), // additional HTTP header length
reinterpret_cast<LPVOID>(buf), // additional data in HTTP Post or HTTP Put
len) // additional data length
&& ::GetLastError()!=12168){
::HeapFree(::GetProcessHeap(), 0, buf);
throw "request failed...";
}
::HeapFree(::GetProcessHeap(), 0, buf);
return true;
}
unsigned long W3Client::GetMultiPartsFormDataLength(){
unsigned long len=0;
if(_listargs.size()>0){
for(list<HTTP_ARG*>::iterator it=_listargs.begin(); it!=_listargs.end(); it++){
HTTP_ARG *p=reinterpret_cast<HTTP_ARG*>(*it);
len+=p->length2();
}
}
return len;
}
unsigned long W3Client::AllocMultiPartsFormData(unsigned char *&buf, const char *szboundry){
unsigned long len=0;
unsigned long ns=GetMultiPartsFormDataLength()+1;
if(buf)
return 0;
buf=reinterpret_cast<unsigned char*>(::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, ns));
if(_listargs.size()>0){
for(list<HTTP_ARG*>::iterator it=_listargs.begin(); it!=_listargs.end(); it++){
HTTP_ARG *pa=reinterpret_cast<HTTP_ARG*>(*it);
len+=pa->dump2(buf+len, ns-len, szboundry);
}
}
memcpy(buf+len, "--", 2);
memcpy(buf+len+2, szboundry, strlen(szboundry));
memcpy(buf+len+2+strlen(szboundry), "--\r\n", 4);
len+=2+strlen(szboundry)+4;
return len;
}
void W3Client::FreeMultiPartsFormData(unsigned char *buf){ ::HeapFree(::GetProcessHeap(), 0, buf); return; }
bool W3Client::RequestPost2(const char *szuri, const char *szref /*=NULL*/){
static LPCTSTR szAcceptType=__HTTP_ACCEPT_TYPE;
static LPCTSTR szContentType="Content-Type: multipart/form-data; boundary=--MULTI-PARTS-FORM-DATA-BOUNDARY\r\n";
unsigned char *buf=NULL;
unsigned long len=0;
if(!_hConnection || _hConnection==INVALID_HANDLE_VALUE)
throw "handle not opened...";
_hRequest=::HttpOpenRequest( _hConnection,
__HTTP_VERB_POST, // HTTP Verb
szuri, // Object Name
HTTP_VERSION, // Version
szref, // Reference
&szAcceptType, // Accept Type
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_FORMS_SUBMIT |
(_t==w3https? INTERNET_FLAG_SECURE:0),
NULL); // context call-back point
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
// REPLACE HEADER
if(!::HttpAddRequestHeaders( _hRequest, __HTTP_ACCEPT, strlen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE))
throw "additional header failed...";
if(!::HttpAddRequestHeaders( _hRequest, szContentType, strlen(szContentType), HTTP_ADDREQ_FLAG_ADD_IF_NEW))
throw "additional header failed...";
// COOKIE ADD
if(_listcookies.size()>0){
string szurl;
switch(_t) {
case w3http:
szurl="http://";
break;
case w3https:
szurl="https://";
break;
}
szurl+=_szaddress.c_str();
if(!(
(_t==w3http && _nport==INTERNET_DEFAULT_HTTP_PORT) ||
(_t==w3https && _nport==INTERNET_DEFAULT_HTTPS_PORT)
)){
char tmp[10]="\0";
sprintf(tmp, ":%d", _nport);
szurl+=tmp;
}
szurl+=szuri;
for(list<HTTP_COOKIE*>::iterator it=_listcookies.begin(); it!=_listcookies.end(); it++){
HTTP_COOKIE *pc=reinterpret_cast<HTTP_COOKIE*>(*it);
if(!::InternetSetCookie(szurl.c_str(), pc->name.c_str(), pc->value.c_str()))
throw "add cookie failed...";
}
}
// build multi-parts/form-data
len=AllocMultiPartsFormData(buf, "--MULTI-PARTS-FORM-DATA-BOUNDARY");
// ADD HEADER CONTENT LENGTH
char szcl[__DEFAULT_BUF_SIZE]="\0";
sprintf(szcl, "Content-Length: %d\r\n", len);
if(!::HttpAddRequestHeaders( _hRequest, szcl, strlen(szcl), HTTP_ADDREQ_FLAG_ADD_IF_NEW))
throw "additional header failed...";
// SEND REQUEST WITH HttpSendRequestEx and InternetWriteFile
static INTERNET_BUFFERS InternetBufferIn={0};
InternetBufferIn.dwStructSize=sizeof(INTERNET_BUFFERS);
InternetBufferIn.Next=NULL;
if(!::HttpSendRequestEx(_hRequest, &InternetBufferIn, NULL, HSR_INITIATE, 0)){
// free
FreeMultiPartsFormData(buf);
throw "request failed";
}
unsigned long nout=0;
DWORD dwOutPostBufferLength=0;
if(!::InternetWriteFile(_hRequest, buf, len, &nout)){
// free
FreeMultiPartsFormData(buf);
throw "request failed";
}
if(!::HttpEndRequest(_hRequest, NULL, HSR_INITIATE, 0)){
// free
FreeMultiPartsFormData(buf);
throw "request failed";
}
// free multi-parts/form-data
FreeMultiPartsFormData(buf);
return true;
}
unsigned long W3Client::QueryCookie(unsigned char *buf, unsigned long len, unsigned long idx /*=0*/){
if(!::HttpQueryInfo(_hRequest, HTTP_QUERY_SET_COOKIE, buf, &len, &idx)){
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "query cookie failed...");
return 0;
}
return len;
}
unsigned long W3Client::QueryContentLength(){
char szt[__DEFAULT_BUF_SIZE]="\0";
unsigned long nread=__DEFAULT_BUF_SIZE;
memset(szt, 0x00, __DEFAULT_BUF_SIZE);
if(!::HttpQueryInfo(_hRequest, HTTP_QUERY_CONTENT_LENGTH , szt, reinterpret_cast<unsigned long*>(&nread), NULL)){
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "query content-length failed...");
return 0;
}
return atol(szt);
}
const char * W3Client::QueryContentType(){
static char szt[__DEFAULT_BUF_SIZE]="\0";
unsigned long nread=__DEFAULT_BUF_SIZE;
memset(szt, 0x00, __DEFAULT_BUF_SIZE);
if(!::HttpQueryInfo(_hRequest, HTTP_QUERY_CONTENT_TYPE , szt, reinterpret_cast<unsigned long*>(&nread), NULL)){
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "query content-type failed...");
return NULL;
}
return const_cast<char*>(szt);
}
unsigned int W3Client::QueryResult() {
char szt[__DEFAULT_BUF_SIZE]="\0";
unsigned long nread=__DEFAULT_BUF_SIZE;
memset(szt, 0x00, __DEFAULT_BUF_SIZE);
if(!::HttpQueryInfo(_hRequest, HTTP_QUERY_STATUS_CODE , szt, reinterpret_cast<unsigned long*>(&nread), NULL)){
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "query status code failed...");
return 404;
}
return atoi(szt);
}
unsigned long W3Client::QueryRawHeader(unsigned char *buf, unsigned long len){
if(!::HttpQueryInfo(_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &len, NULL)){
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "query cookie failed...");
return 0;
}
return len;
}
unsigned long W3Client::Response(unsigned char *buf, unsigned long len){
unsigned long nread=0;
try{
if(!_hRequest)
throw "connection failed...";
if(!::InternetReadFile(_hRequest, buf, len, &nread))
throw "response failed...";
}catch(const char *szm){
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), szm);
}catch(...){
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
__w3cexcept(_szaddress.c_str(), _nport, _t, _szuri.c_str(), "unknown exception...");
}
return nread;
}
bool W3Client::GetFile(const char *szuri, const char *szfile, bool ascii /*=false*/){
bool r=true;
try{
if(!_hOpen || !_hConnection)
throw "connection failed";
_hRequest=::FtpOpenFile(_hConnection,
szuri,
GENERIC_READ,
(ascii? INTERNET_FLAG_TRANSFER_ASCII : INTERNET_FLAG_TRANSFER_BINARY),
NULL);
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
HANDLE f=::CreateFile(szfile, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
unsigned char buf[__DEFAULT_BUF_SIZE]="\0";
unsigned long nread=0;
if(f){
while(::InternetReadFile(_hRequest, buf, __DEFAULT_BUF_SIZE, &nread) && nread>0)
::WriteFile(f, buf, nread, &nread, NULL);
::CloseHandle(f);
}
}catch(const char *szm){
r=false;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, szm);
}catch(...){
r=false;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, "unknown exception...");
}
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
return r;
}
unsigned long W3Client::GetFile(const char *szuri, unsigned char *buf, unsigned long len, bool ascii /*=false*/){
unsigned long nread=0;
try{
if(!_hOpen || !_hConnection)
throw "connection failed";
if(!_hRequest)
_hRequest=::FtpOpenFile(_hConnection,
szuri,
GENERIC_READ,
(ascii? INTERNET_FLAG_TRANSFER_ASCII : INTERNET_FLAG_TRANSFER_BINARY),
NULL);
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
if(!::InternetReadFile(_hRequest, buf, len, &nread) || nread<=0){
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
}
}catch(const char *szm){
nread=0;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, szm);
}catch(...){
nread=0;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, "unknown exception...");
}
return nread;
}
bool W3Client::PutFile(const char *szuri, const char *szfile, bool ascii /*=false*/){
bool r=true;
try{
if(!_hOpen || !_hConnection)
throw "connection failed";
_hRequest=::FtpOpenFile(_hConnection,
szuri,
GENERIC_WRITE,
(ascii? INTERNET_FLAG_TRANSFER_ASCII : INTERNET_FLAG_TRANSFER_BINARY),
NULL);
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
HANDLE f=::CreateFile(szfile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
unsigned char buf[__DEFAULT_BUF_SIZE]="\0";
unsigned long nread=0;
if(f){
while(::ReadFile(f, buf, __DEFAULT_BUF_SIZE, &nread, 0) && nread>0)
::InternetWriteFile(_hRequest, buf, nread, &nread);
::CloseHandle(f);
}
}catch(const char *szm){
r=false;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, szm);
}catch(...){
r=false;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, "unknown exception...");
}
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
return r;
}
unsigned long W3Client::PutFile(const char *szuri, unsigned char *buf, unsigned long len, bool ascii){
unsigned long nread=0;
try{
if(!_hOpen || !_hConnection)
throw "connection failed";
if(!_hRequest)
_hRequest=::FtpOpenFile(_hConnection,
szuri,
GENERIC_WRITE,
(ascii? INTERNET_FLAG_TRANSFER_ASCII : INTERNET_FLAG_TRANSFER_BINARY),
NULL);
if(!_hRequest || _hRequest==INVALID_HANDLE_VALUE)
throw "request failed...";
if(!::InternetWriteFile(_hRequest, buf, nread, &nread) || nread<=0){
::InternetCloseHandle(_hRequest);
_hRequest=NULL;
}
}catch(const char *szm){
nread=0;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, szm);
}catch(...){
nread=0;
__w3cexcept(_szaddress.c_str(), _nport, _t, szuri, "unknown exception...");
}
return nread;
}
void __w3curlparse(const char *szurl,
char *szprotocol, char *szuser, char *szpassword,
char *szaddress, unsigned long &nport, char *szuri){
char szport[1024]="\0";
unsigned long npos=0;
bool bflag=false;
while(strlen(szurl)>0 && npos<strlen(szurl) && strncmp((szurl+npos), ":", 1))
++npos;
if(!strncmp((szurl+npos+1), "/", 1)){ // is protocol
if(szprotocol){
strncpy(szprotocol, szurl, npos);
szprotocol[npos]=0;
}
bflag=true;
}else{ // is host
if(szprotocol){
strncpy(szprotocol, "http", 4);
szprotocol[5]='\0';
}
}
unsigned long nsp=0, usp=0;
if(bflag){
usp=nsp=npos+=3;
}else{
usp=nsp=npos=0;
}
while(strlen(szurl)>0 && usp<strlen(szurl) && strncmp((szurl+usp), "@", 1))
++usp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -