EDN Admin
Well-known member
Hi, i was wondering if someone could show me how to log into a website using c++ .net? I want to login to say megaupload.com for example, now i have to code in c++ but how could i do it in managed c++? A link or something would be great, i see many examples of this in c# but not in c++ =/. Is there a way to call this code in .net?<br/> <br/> I have already got my download code working in managed c++(gui) and i can download files from urls but i want to try to be able to get files from sites that require a login to download directly. <br/> <br/> <br/>
<pre>#include <winsock2.h>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#define HOST "<a title="Linkification: http://www.megaupload.com http://www.megaupload.com" title="Linkification: http://www.megaupload.com www.megaupload.com
"
#define PORT 80
#define LEN 534 // Points to contenth length in http_req[]
int hconnect(char* host,int port);
int s=0;
char http_req[]=
"POST /?c=login HTTP/1.1rn"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */* rn"
"Referer: <a title="Linkification: http://megaupload.com/?c=login http://megaupload.com/?c=login" title="Linkification: http://megaupload.com/?c=login http://megaupload.com/?c=login
rn"
"Accept-Language: en-USrn"
"Content-Type: application/x-www-form-urlencodedrn"
"UA-CPU: x86rn"
"Accept-Encoding: gzip, deflatern"
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)rn"
"Host: megaupload.comrn"
"Content-Length: 00rn" // LEN points here!!!
"Connection: Keep-Alivern"
"Cache-Control: no-cachernrn";
char http_line_req[]="login=1&redir=1&username=usrname&password=passrnrn"; // fill in here
char conn[1024];
int nLen=0;
char szLen[2];
void main()
{
nLen=strlen(http_line_req);
itoa(nLen,szLen,10);
memcpy(http_req+LEN,szLen,2);
s=hconnect(HOST,PORT);
send(s,http_req,strlen(http_req),NULL);
send(s,http_line_req,nLen,NULL);
recv(s,conn,1024,NULL);
//TODO: continue recving, that 1KB buffer is not enough to recv all the data
}
int hconnect(char* host,int port)
{
WSAData wsa;
WSAStartup(MAKEWORD(1,1),&wsa);
struct sockaddr_in sai;
struct hostent *hp;
memset(&sai,0,sizeof(struct sockaddr_in));
hp=gethostbyname(host);
sai.sin_family=AF_INET;
sai.sin_addr=*((struct in_addr *)hp->h_addr);
sai.sin_port=htons(port);
SOCKET s;
s=socket(AF_INET,SOCK_STREAM,0);
connect(s,(struct sockaddr*)&sai,sizeof(sockaddr));
return s;
}
[/code]
View the full article
<pre>#include <winsock2.h>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#define HOST "<a title="Linkification: http://www.megaupload.com http://www.megaupload.com" title="Linkification: http://www.megaupload.com www.megaupload.com
"
#define PORT 80
#define LEN 534 // Points to contenth length in http_req[]
int hconnect(char* host,int port);
int s=0;
char http_req[]=
"POST /?c=login HTTP/1.1rn"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */* rn"
"Referer: <a title="Linkification: http://megaupload.com/?c=login http://megaupload.com/?c=login" title="Linkification: http://megaupload.com/?c=login http://megaupload.com/?c=login
rn"
"Accept-Language: en-USrn"
"Content-Type: application/x-www-form-urlencodedrn"
"UA-CPU: x86rn"
"Accept-Encoding: gzip, deflatern"
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)rn"
"Host: megaupload.comrn"
"Content-Length: 00rn" // LEN points here!!!
"Connection: Keep-Alivern"
"Cache-Control: no-cachernrn";
char http_line_req[]="login=1&redir=1&username=usrname&password=passrnrn"; // fill in here
char conn[1024];
int nLen=0;
char szLen[2];
void main()
{
nLen=strlen(http_line_req);
itoa(nLen,szLen,10);
memcpy(http_req+LEN,szLen,2);
s=hconnect(HOST,PORT);
send(s,http_req,strlen(http_req),NULL);
send(s,http_line_req,nLen,NULL);
recv(s,conn,1024,NULL);
//TODO: continue recving, that 1KB buffer is not enough to recv all the data
}
int hconnect(char* host,int port)
{
WSAData wsa;
WSAStartup(MAKEWORD(1,1),&wsa);
struct sockaddr_in sai;
struct hostent *hp;
memset(&sai,0,sizeof(struct sockaddr_in));
hp=gethostbyname(host);
sai.sin_family=AF_INET;
sai.sin_addr=*((struct in_addr *)hp->h_addr);
sai.sin_port=htons(port);
SOCKET s;
s=socket(AF_INET,SOCK_STREAM,0);
connect(s,(struct sockaddr*)&sai,sizeof(sockaddr));
return s;
}
[/code]
View the full article