How to Use the wininet interface for YouTube user name and password authentication
# Include "stdafx. H"
# Include <windows. h>
# Include <wininet. h>
# Include <tchar. h>
# Include <strsafe. h>
# Include <string>
Using namespace STD;
# Pragma comment (Lib, "wininet. lib ")
Lpctstr lpszaccepttype = l "accept: text /*";
Char g_szauth [1024];
Tchar g_szyoutubeuser [128];
Hinternet g_hsession = NULL;
Hinternet g_hconnect = NULL;
Hinternet g_hrequest = NULL;
Bool clientlogin (lptstr lpuser, lptstr lppassword );
Void logout ();
Int _ tmain (INT argc, _ tchar * argv [])
{
Clientlogin (L "", l "");
Printf (g_szauth );
Printf ("/N ");
Logout ();
Return 0;
}
Bool clientlogin (lptstr lpuser, lptstr lppassword)
{
Bool bloginsuceess = false;
G_hsession = internetopen (null, internet_open_type_preconfig, null, null, 0 );
If (! G_hsession)
{
Return bloginsuceess;
}
Lptstr lpszhost = l "www.google.com ";
G_hconnect = internetconnect (g_hsession, lpszhost, internet_default_http_port, null, null, internet_service_http, 0, 0 );
If (! G_hconnect)
{
Return bloginsuceess;
}
Lptstr lpszobjectnameformat = l "/accounts/clientlogin? Accounttype = hosted_or_google & Email = % S & passwd = % S & service = xapi & source = Wondershare-MovieStroy-v4.5.0 ";
Tchar szobjectname [1024];
Memset (szobjectname, 0, sizeof (szobjectname ));
Stringcchprintf (szobjectname, 1024, lpszobjectnameformat, lpuser, lppassword );
G_hrequest = httpopenrequest (g_hconnect, l "Post", szobjectname, null, null, & lpszaccepttype, internet_flag_reload, 0 );
If (! G_hrequest)
{
Return bloginsuceess;
}
Const tchar szheaders [] = l "Content-Type: Application/X-WWW-form-urlencoded ";
Bool bsendrequest = httpsendrequest (g_hrequest, szheaders, lstrlen (szheaders), null, 0 );
If (! Bsendrequest)
{
Return bloginsuceess;
}
DWORD dwsize;
Lpstr lpszdata; // buffer for the data
DWORD dwdownloaded; // size of the downloaded data
If (! Internetquerydataavailable (g_hrequest, & dwsize, 0, 0 ))
{
Return bloginsuceess;
}
Else
{
Lpszdata = new char [dwsize + 1];
If (! Internetreadfile (g_hrequest,
(Lpvoid) lpszdata,
Dwsize,
& Dwdownloaded ))
{
Delete [] lpszdata;
Return bloginsuceess;
}
Else
{
// Add a null terminator to the end of the Data Buffer
Lpszdata [dwdownloaded] = '/0 ';
String strdata = lpszdata;
Int Index = strdata. Find ("auth ");
Bloginsuceess = index! =-1? True: false;
Memset (g_szauth, 0, sizeof (g_szauth ));
Copymemory (g_szauth, lpszdata + index, dwdownloaded-(index + 1 ));
G_szauth [dwDownloaded-index-1] = '/0 ';
Delete [] lpszdata;
}
}
Return bloginsuceess;
}
Void logout ()
{
Internetclosehandle (g_hrequest );
Internetclosehandle (g_hconnect );
Internetclosehandle (g_hsession );
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wxtsmart/archive/2008/09/19/2953674.aspx