You need to request the login page, save the cookies and provide the username and password with the login token. There are some mediawiki frameworks which already take care of these details.
Alternatively, you can use the maintenance/importImages.php script on the server.