|
bool W3Client::RequestGet(const char *szuri, const char *szref /*=NULL*/){
static LPCTSTR szAcceptType=TEXT(__HTTP_ACCEPT_TYPE);
if(!_hConnection || _hConnection==INVALID_HANDLE_VALUE)
throw "handle not opened...";
_hRequest=::HttpOpenRequest(_hConnection,
__HTTP_VERB_GET, // HTTP Verb
szuri, // Object Name
HTTP_VERSION, // Version
szref, // Reference
&szAcceptType, // Accept Type
//INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_RELOAD | // Forces the page to come from the web, not cache
INTERNET_FLAG_NO_AUTO_REDIRECT | // Don't follow redirects
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | // Disables the ability of the Win32 Internet functions to detect this special type of redirect
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | // Disables the ability of the Win32 Internet functions to detect this special type of redirect
INTERNET_FLAG_IGNORE_CERT_CN_INVALID | // Ignore invalid certificate
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | // Ignore invalid certificate date
(_t==w3https? INTERNET_FLAG_SECURE ), // Secure if neccessary
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 "new header failed...";
// Add "Accept-Encoding: gzip, deflate"
if(!::HttpAddRequestHeaders(_hRequest,
"Accept-Encoding: gzip, deflate\r\n",
-1,
HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE)) // Replace if exists or add if doesn't
throw "additional header failed...";
// Unzip the data coming back if neccessary
#define INTERNET_OPTION_HTTP_DECODING 65 // Our header file is too old to include this, but this works OK. Documentation says that it only works from Vista onwards, but works OK for XP.
int Flag;
Flag = 1;
if(!::InternetSetOption(_hRequest,
INTERNET_OPTION_HTTP_DECODING,
&Flag,
4))
throw "INTERNET_OPTION_HTTP_DECODING 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...";
}
}
// SEND REQUEST
if(!::HttpSendRequest( _hRequest, // handle by returned HttpOpenRequest
NULL, // additional HTTP header
0, // additional HTTP header length
NULL, // additional data in HTTP Post or HTTP Put
0) // additional data length
)
throw "request failed...";
return true;
}
|
|
|
|
|
This code looks as though it was written for Visual C++ version 6 (possibly also 2002), but it worked out of the box. It is a little bit light on comments.
Eric
|
|
|
|
|
I am using the VS2003 to compile the source code. I got the error message
"c:\Documents and Settings\Guaz Wei Kiat\Desktop\HTTPS\w3csample\w3c.cpp(1158): error C2248: 'W3Client::HTTP_COOKIE' : cannot access private typedef declared in class 'W3Client'"
please help me! thanks
|
|
|
|
|
Add:
public:
before:
typedef struct __HTTP_COOKIE {
std::string name;
std::string value;
__HTTP_COOKIE(const __HTTP_COOKIE& c): name(c.name), value(c.value) {}
__HTTP_COOKIE(const char *szn, const char*szv): name(szn), value(szv) {}
__HTTP_COOKIE(const char *szn, const int v): name(szn) { char t[15]="\0"; sprintf(t, "%d", v); value=t; }
__HTTP_COOKIE(const char *szn, const long v): name(szn) { char t[15]="\0"; sprintf(t, "%d", v); value=t; }
__HTTP_COOKIE(const char *szn, const float v): name(szn) { char t[15]="\0"; sprintf(t, "%f", v); value=t; }
__HTTP_COOKIE(const char *szn, const double v): name(szn) { char t[15]="\0"; sprintf(t, "%f", v); value=t; }
} HTTP_COOKIE, *PHTTP_COOKIE;
|
|
|
|
|
Hello Yongseon,
While searching for Visual C++ based HTTPS samples on Google, I found the sample code "(WWW (HTTP/HTTPS/FTP) Client using WININET)" posted by you. Thank you very much for your sample as it does what I was looking for. I want to explore the possibility of using your code in my program.
The code has no copyright or licensing information. Following is specified in the License section of the Sample
"License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below."
I'll appreciate if you can clarify if there are any licensing/copyright restrictions considering that I will be integrating the code in my product and may be required to modify the code.
Thanks and Regards,
Prasanna
|
|
|
|
|
thanks.
license is changed.
|
|
|
|
|
1'st - thanks for the great article. It really helped me a lot.
There is a small issue with cookies: the code does not actually set the cookies for the session. when a cookie is added it is simply placed on the list, and before the connection is started the elements of the list are traversed but not actually used for anything.
I modified the code as follows, and it seems to do the trick:
void W3Client::InitializeCookies() {
if(_listcookies.size()>0){
for(list<HTTP_COOKIE*>::iterator it=_listcookies.begin(); it!=_listcookies.end(); it++){
HTTP_COOKIE *p=reinterpret_cast<HTTP_COOKIE*>(*it);
InternetSetCookie(p->name.c_str(), NULL, p->value.c_str());
delete p;
}
_listcookies.clear();
}
return;
}
|
|
|
|
|
I use AsyncW3Client to try async function like the following code.
AsyncW3Client w;
if(w.Connect("http://atnotes.free.fr/zips/tradchin.zip", __getstatus)){
if(w.Request("/")){
TCHAR buf[1024]="\0";
while( w.Response(reinterpret_cast<unsigned char="" *="">(buf), 1024))
cout << buf << endl;
}
}
I have some bugs/question on it.
1. The w.Request always reads nothing.
2. No receiving INTERNET_STATUS_REQUEST_COMPLETE event on callback function.
Please help me on this, thank you all very much.
|
|
|
|
|
How to implemente a Timeout with W3C ?
|
|
|
|
|
You POST Method does NOT put in any BOUNDARY for uploading files.
How would you use this to upload files to a server using multipart-form data?
When you upload files from a form in a web page you have to specify a BOUNDARY condition ???
Could you give an example of how to upload a file using POST to upload an image file with a boundary specified?
Thanks!
Great Code!
Bill SerGio
|
|
|
|
|
Any chance you found an answer?
|
|
|
|
|
Hi,
It is fine for compiler. When I included your source code (w3c.cpp and w3c.h) in my project, I got an error on "_szuri = szuri;".
I found _szuri = {???}. Why did it not be initialized?
Do I miss some setting?
It works fine on your sample.
Thank you!
|
|
|
|
|
I'm always running into the "unknow exception" Any ideas?
|
|
|
|
|
Hi all!
File w3c.cpp, Line 205:
...
if(_hOpen || _hOpen!=INVALID_HANDLE_VALUE)
::InternetCloseHandle(_hOpen);
...
(INVALID_HANDLE_VALUE = -1)
Can we replace by ?
if(_hOpen && _hOpen!=INVALID_HANDLE_VALUE)
::InternetCloseHandle(_hOpen);
if _hOpen is NULL, then we will not close it.
Lines 207-208, 212-213, 214-215, ... are same error.
|
|
|
|
|
Hello!
First at all thank you for this classes, i like to use it
But it looks like it is a bug here. I founded it, when i
install IE 7 beta (with new version of wininet.dll in there).
In methods RequestGet, RequestPost and RequestPost2
you use ::HttpOpenRequest with wrong param szAcceptType:
static LPCTSTR szAcceptType=TEXT(__HTTP_ACCEPT_TYPE);
...
...
_hRequest=::HttpOpenRequest( _hConnection, __HTTP_VERB_GET, // HTTP Verb szuri, // Object Name HTTP_VERSION, // Version szref, // Reference &szAcceptType, // Accept Type INTERNET_FLAG_NO_CACHE_WRITE | https? INTERNET_FLAG_SECURE ), NULL); // context call-back point.
But "Accept Type"-parameter must be (from MSDN)
lplpszAcceptTypes
[in] Long pointer to a null-terminated (!!!) array of string pointers indicating content types accepted by the client. If this parameter is NULL, no types are accepted by the client. Servers interpret a lack of accept types to indicate that the client accepts only documents of type "text/*" (that is, only text documents, and not pictures or other binary files).
So, i think - right will be something, like this:
LPCTSTR szAcceptTypes[]={TEXT(__HTTP_ACCEPT_TYPE),
NULL};
...
...
_hRequest=::HttpOpenRequest( _hConnection, __HTTP_VERB_GET, // HTTP Verb szuri, // Object Name HTTP_VERSION, // Version szref, // Reference szAcceptTypes, // Accept Type INTERNET_FLAG_NO_CACHE_WRITE | https? INTERNET_FLAG_SECURE ), NULL); // context call-back point.
Sorry for my bad English, i learned German )
Wbr, Kirill Sbytov.
-- modified at 5:56 Thursday 29th June, 2006
|
|
|
|
|
I am recieving error 12006 when the ::HttpOpenRequest is called
with wininet.dll (7.0.5450.4) installed
ERROR_INTERNET_UNRECOGNIZED_SCHEME
12006
The URL scheme could not be recognized, or is not supported.
Does anyone have a clue how to fix this?
|
|
|
|
|
Thanks to the author for very useful classes. Thanks to Kirill Sbytov for solution of the HttpOpenRequest call.
|
|
|
|
|
now i can login in yahoo email use this class(i send data to the another web,it will help me login in ).
but now i don't know how to login out the yahoo email?
|
|
|
|
|
I keep getting that IWORKERTHREAD is an undefined identifier and it can't find wt.h in VC6 and VC7. Any ideas?
-- modified at 13:37 Tuesday 20th June, 2006
|
|
|
|
|
nevermind I don't need to use asynchronous.
|
|
|
|
|
|
The Dialog and String Table are all in KOREAN
Could someone tell me How do I convert it to English ?
Many Thanks In advance
|
|
|
|
|
How do i use sessions with this client? What i have is a login page that redirects me to an upload page
|
|
|
|
|
sessions are Server's Values.
after connect your server, if you have don't close(), you should be use sessions.
good luck.
|
|
|
|
|
W3Client w3;
CString szheader;
CString szsource;
w3.SetAskInstallCert(m_bAskInstallCert);
if(w3.Connect((const char*)m_sUrl)){
if(w3.Request(w3.GetURI())){
unsigned char buf[1024]="\0";
unsigned char buf2[1025]="\0";// 1025 is "\0"
w3.QueryRawHeader(buf, 1024);
szheader=buf;
memset(buf, 0x00, 1024);
int read = 0;
while(read = w3.Response(buf, 1024)){
memset(buf2, 0x00, 1025);
memset(buf2, 65, read);
szsource+=buf2;
memcpy(szsource.GetBuffer(szsource.GetLength()) + szsource.GetLength() - read, buf, read);
memset(buf, 0x00, 1024);
}
HANDLE file=(HANDLE)::CreateFile(m_sFileDownload, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
unsigned long nw=0;
WriteFile(file, (const char*)szsource, szsource.GetLength(), &nw, NULL);
::CloseHandle(file);
}
w3.Close();
}
--------------------------
ERP VN: www.erpvn.net
|
|
|
|
|