How do I interact with websites in C++?
For example, a website has a dropbox, text area and a button, and I want my program to fill text inside the text area, choose an option from the dropbox, and make the button fire its event("clicking" it).
How can I achieve something like that?
thanks!
LibOnion is a lightweight C library that helps create web servers in the C programming language. It's based on request handlers that it uses to process all requests. The handlers may be nested, creating onion-like layers. The first layer will generally check the server name.
Browser initiates TCP connection with the server. Browser sends the HTTP request to the server. Server processes request and sends back a response. Browser renders the content.
The Web server returns the page to the IP address specified by the browser requesting the page. The page may also contain links to other files on the same server, such as images, which the browser will also request. The browser collects all the information and displays to your computer in the form of Web page.
When you type a web address in your browser, the browser looks at the DNS to find the website's IP address before it can retrieve the website. The browser needs to find out which server the website lives on, so it can send HTTP messages to the right place (see below).
First you have to understand that on the server there is no text box or button.
These are constructs that are built by the browser to display to you.
The browser will then take user input into the text box and interprets the clicks on the button. What happens (usually) when the button is clicked is that the browser sends an HTML "POST" request to the server. The browser builds the post request based on what the user has done in the UI.
Example:
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title>TestDoc</title></head>
<body>
<form action="http://website.com/form.html" method="post">
<div>
<textarea name="userinfo" rows="2" cols="30">Some Text</textarea>
<input type="submit">
</div>
</form>
</body>
</html>
Your browser interprets this and displays a text box and submit button. When you press the submit button the browser builds an HTTP post command that is sent back to the browser. It connects to the website at http://website.com/form.html
(see form tag in code snippet above) and sends the content of the text area (tagged with the value userinfo
).
You can manually do the same.
But you need to understand what values the website is expecting and build the appropriate command based on what the website is expecting. To do this, the easiest way is to use libCurl. The documentation for this package explains in detail how to build a post request.
Here is a post example:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With