Use libcurl to sign into webpage?

See both C++ and page source code below. I want to use libcurl to enter a username and password into the appropriate fields on the webpage and then click the submit button. What changes to the code below do I need to make?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>


int main()
{

curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle = curl_easy_init ( );

// Set up a couple initial paramaters that we will not need to mofiy later.
curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");

// Visit the login page once to obtain a PHPSESSID cookie
curl_easy_setopt(myHandle, CURLOPT_URL, "my website");


curl_easy_perform( myHandle );


// Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
curl_easy_setopt(myHandle, CURLOPT_REFERER, "my website");
// Next we tell LibCurl what HTTP POST data to submit
char *data="Name/Badge=1234&Password=1234";
curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, data);

//curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, 'ctl00$LogInButton[]=Sign in');


curl_easy_perform( myHandle );
curl_easy_cleanup( myHandle );






return 0;
}






HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<div id="loginForm">
    <div id="loginInput">
        <label for="ctl00_LoginTextBox" id="ctl00_LoginLabel">Name/Badge</label>
	    <input name="ctl00$LoginTextBox" type="text" id="ctl00_LoginTextBox" class="PopupInput" />
	</div>
	<div id="passwordInput">
	    <label for="ctl00_PasswordTextbox" id="ctl00_PasswordLabel">Password</label>
	    <input name="ctl00$PasswordTextbox" type="password" id="ctl00_PasswordTextbox" class="PopupInput" />	    
	</div>
	<div id="equipmentbox">
        
        
        
        
    </div>
</div>


<div id="ctl00_LoginButtons">
		
	<input type="submit" name="ctl00$LogInButton" value="Sign in" id="ctl00_LogInButton" class="btn" />
	
	<input type="submit" name="ctl00$SwitchLoginScreenModeBT" value="Change Password" id="ctl00_SwitchLoginScreenModeBT" class="btn" />	

libcurl doesn't know anything about buttons and forms and...
You just pass the form data, submit the request and receive a response.
Then you process the response.
You can use libcurl to log in to a secure website. Use the following functions to do it.
CURLOPT_FOLLOWLOCATION-Server redirection
CURLOPT_POST- This function tells libcurl to switch into post mode.
CURLOPT_POSTFIELDS-This function tells to set the post fields.
CURLOPT_USERAGENT-Set a simple user agent
To know more visit Microsoft Support UK(https://microsoftsupport.co/microsoft-support-uk). they will help you using libcurl.
Topic archived. No new replies allowed.