locked
Log in to a website managed c++ RRS feed

  • Question

  • 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?

    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. 


    #include <winsock2.h>
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    
    
    #pragma comment(lib,"ws2_32.lib")
    
    #define HOST "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.1\r\n"
    "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, */* \r\n"
    "Referer: http://megaupload.com/?c=login
    \r\n"
    "Accept-Language: en-US\r\n"
    "Content-Type: application/x-www-form-urlencoded\r\n"
    "UA-CPU: x86\r\n"
    "Accept-Encoding: gzip, deflate\r\n"
    "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)\r\n"
    "Host: megaupload.com\r\n"
    "Content-Length: 00\r\n" // LEN points here!!!
    "Connection: Keep-Alive\r\n"
    "Cache-Control: no-cache\r\n\r\n";
    
    char http_line_req[]="login=1&redir=1&username=usrname&password=pass\r\n\r\n"; // 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 recv'ing, 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;
    }
    
    Friday, January 22, 2010 3:33 AM

Answers

  • There is no correct way to log on a server. The method to log on is decided by the server, not by the technology you use. You are better off contact the server administrator to know if you can get any kind of API. Otherwise you need to do (potentially extensive) reverse engineering the protocol by watching the traffic when you log on in a browser. Nothing in Visual C++ can help you to do that.
    Once you find the protocol you can replay the log on conversation in any networking library. For how to use the one in System.Net, visit the System.Net forum

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Friday, January 22, 2010 5:49 PM

All replies

  • Hello cruisx,

    Welcome to MSDN forum.

    I am not sure what you mean by " i see many examples of this in c# but not in c++ =/. Is there a way to call this code in .net?". Did you mean you can do it in C#? If so, it it easy to translate C# code into C++\CLI, we can get use of Reflector tool

    http://www.red-gate.com/products/reflector/

    Or we can wrap the function in a C#(.NET) assembly, and call it in C++\CLI.

    Regards,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 22, 2010 10:07 AM
  • Hi, thaks for the reply. I was asking how i would log in to a website in managed c++. Well this is what i have come up with, could someone help me out as it does not quite work. It gives a invalid value error and i dont think its logging in correctly.

    I click button 2 to log in and then click button 1 to download the file.

    #pragma once
    
    
    namespace downloadtest {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::Net;
    	using namespace System::IO;
    	using namespace System::Text;
    
    
    
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::Button^  button1;
    	public: System::Windows::Forms::ProgressBar^  pb;
    
    	private: System::Windows::Forms::Label^  label1;
    	private: System::Windows::Forms::Button^  button2;
    	protected: 
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->pb = (gcnew System::Windows::Forms::ProgressBar());
    			this->label1 = (gcnew System::Windows::Forms::Label());
    			this->button2 = (gcnew System::Windows::Forms::Button());
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(263, 277);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(75, 23);
    			this->button1->TabIndex = 0;
    			this->button1->Text = L"button1";
    			this->button1->UseVisualStyleBackColor = true;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// pb
    			// 
    			this->pb->Location = System::Drawing::Point(243, 181);
    			this->pb->Name = L"pb";
    			this->pb->Size = System::Drawing::Size(216, 35);
    			this->pb->TabIndex = 1;
    			// 
    			// label1
    			// 
    			this->label1->AutoSize = true;
    			this->label1->Location = System::Drawing::Point(542, 66);
    			this->label1->Name = L"label1";
    			this->label1->Size = System::Drawing::Size(35, 13);
    			this->label1->TabIndex = 2;
    			this->label1->Text = L"label1";
    			// 
    			// button2
    			// 
    			this->button2->Location = System::Drawing::Point(344, 277);
    			this->button2->Name = L"button2";
    			this->button2->Size = System::Drawing::Size(75, 23);
    			this->button2->TabIndex = 3;
    			this->button2->Text = L"button2";
    			this->button2->UseVisualStyleBackColor = true;
    			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(675, 338);
    			this->Controls->Add(this->button2);
    			this->Controls->Add(this->label1);
    			this->Controls->Add(this->pb);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    
    
    	public:  void  ProgressChanged(System::Object ^sender, System::Net::DownloadProgressChangedEventArgs^ e)
    			 {
    
    
    				 double bytesIn = double::Parse(e->BytesReceived.ToString());
    				 double totalBytes = double::Parse(e->TotalBytesToReceive.ToString());
    				 double percentage = bytesIn / totalBytes * 100;
    
    				 pb->Value = int::Parse(Math::Truncate(percentage).ToString());
    
    
    
    			 }
    
    			 void login()
    			 {
    
    				 String ^ postData = "login=1&redir=1&username= MYNAME &password= MYPASS ";//No spacing focoruse.
    
    				 HttpWebRequest ^request =dynamic_cast<HttpWebRequest^>( WebRequest::Create("http://megaupload.com/?c=login"));
    				 request->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");
    				 request->Referer=("http://megaupload.com/?c=login");
    				 request->ContentType=("application/x-www-form-urlencoded");
    
    				 request->UserAgent=(" Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
    				 request->ContentLength = (postData->Length);
    				 request->KeepAlive = true;
    				 request->Method = ("POST");
    				 request->AllowAutoRedirect =  true;
    
    				 Encoding^ en = Encoding::ASCII;
    
    				 array<Byte> ^postBytes;
    				 Stream ^requestStream = request->GetRequestStream();
    				 postBytes  = en->ASCII->GetBytes(postData);
    				 requestStream->Write(postBytes,0,postBytes->Length);
    				 //requestStream->Close();
    
    
    
    			 }
    
    
    	private: System::Void button1_Click(System::Object^ , System::EventArgs^  e)
    			 {
    
    
    				
    				 // this works fine as its on server,no pass required
    				 // String ^url = "http://mozilla-mirror.3347.voxcdn.com/pub/mozilla.org/firefox/releases/3.6/win32/en-US/Firefox%20Setup%203.6.exe";
    				 //needs pass to DL.
    				 String ^url = "http://www.megaupload.com/?d=JSAOZUJL";
    				 WebClient ^client = gcnew WebClient();
    				 client->Proxy = nullptr;				
    				 client->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this,&downloadtest::Form1::ProgressChanged); 
    
    				 client->DownloadFileAsync(gcnew Uri(url),"c:\\FACING_ALI\\brush.abr");
    
    
    			 }
    	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    				 login();
    			 }
    	};
    }
    
    
    Friday, January 22, 2010 4:24 PM
  • There is no correct way to log on a server. The method to log on is decided by the server, not by the technology you use. You are better off contact the server administrator to know if you can get any kind of API. Otherwise you need to do (potentially extensive) reverse engineering the protocol by watching the traffic when you log on in a browser. Nothing in Visual C++ can help you to do that.
    Once you find the protocol you can replay the log on conversation in any networking library. For how to use the one in System.Net, visit the System.Net forum

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Friday, January 22, 2010 5:49 PM
  • Hi all,

    I'm writting a log in vault in vc++.

    Could you refer me to any example of a traffic listener by a callback or something similar when a log in is produced?

    Also I would like know how to save the data and how to use it to relogin later.

    Any help will be highly appreciated.

     

    Regards,

    Miguel

     

    Wednesday, March 9, 2011 7:56 PM
  • http://blogs.msdn.com/b/deviations/archive/2010/06/22/how-to-user-fiddler-and-http-replay-to-have-an-offline-copy-of-your-site.aspx

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, March 10, 2011 2:10 AM
  • Thanks, I found util STRACE.

    The approach I was looking for is to watch all internet traffic through WinSock2 and detect logins, what already does STRACE..

    You said

    "you need to do (potentially extensive) reverse engineering the protocol by watching the traffic when you log on in a browser. Nothing in Visual C++ can help you to do that."

    So, the point is, when I have the log file from STRACE, is it possible to reproduce the actions in the browser?

    What I would like to achieve is that after a user validation in my program, when he wants to log in again in a place that succesfully logged in before, the program does the login and shows the next step of the login in the browser (as I suppose that STRACE or any traffic watching tool retrieves encripted data, so it is not possible to catch user and pw, and is because of this that you said doing reverse engineering?..)

     

    Thursday, March 10, 2011 12:27 PM
  • It depends on that particular web site, for example my company's web site has a requirement to make replaying impractical to avoid man-in-the-middle attacks.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, March 11, 2011 8:20 PM