how can I connect hotspot using API?

Hello ,
I’m writing a small program in C#
that I want to connect to my hotspot1-login-then I will be able to run some test with my code.

how can I do this?
what I need to write in the code in order to register to the hotspot ?
I know my user and pass in my hotspot system

Thanks ,

If i understand what you want to do,

Do you want to test your captive portal out of your MikroTik network?

Maybe you can emulate a virtual host and connect a Layer 2 tunnel (EoIP) through CHR router to your MikroTIk Hotspot Router.

No
I will expalin :
I want to write a code that check my router hotspot
so when i will run it it will send the username&password to the router and connect to the hotspot
is it clear what I want to do?

You mean you want to login to the hotspot?

If it’s solely for the purposes of your server being able to connect to the router and do something else with the API, the best way is to simply create a “bypassed” binding for the server in “/ip hotspot ip-binding”. That way, you don’t have to login at all - the API app will “just work”.

Yes , this is what I want to do
and how do I do it?
do you have an example?
what do I need to define in the router and what in the API?

also I don’t know from which computer MAC I will work - does it mettar?

Thanks ,

You need to either have a fixed MAC address or a fixed IP address.

The command is simply

/ip hotspot ip-binding add type=bypassed mac-address="01:23:45:67:89:AB" address=192.168.88.254 to-address=192.168.88.254

and you can omit either the “mac-address” or the “address” arguments, and adjust the values accordingly.

The “to-address” argument is the IP address that the router will treat you as after login, while “address” is the one it identifies you with (prior to login).

The “type” is the key bit - “bypassed” means you don’t need to login. You’re automatically logged in (ala MAC authentication) with an internal IP specified in “to-address”.

Once you have that binding, you don’t need to do anything at the API app.


ALTERNATIVELY

If you want to be able to run the app from any device in the network, don’t do a binding. Instead, from the API app, you need to make an HTTP request within it to “http:///login?username=&password=”. In C#, you can do so with System.Net.WebRequest. Once the request is executed, the device will be authenticated with the specified credentials, thus being able to connect to the router through that. You’ll of course want to do a request to “http:///logout?erase-cookie=yes” if you want the device’s real owner to login after your app is done, instead of letting them use your credentials.

Great ,
This is what i wanted to do
So this is what i need to write?

Uri ourUri = new Uri("http://192.168.100.254/login?username=david&password=david");         

// Create a 'WebRequest' object with the specified url. 
WebRequest myWebRequest = WebRequest.Create(url); 

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if (ourUri.Equals(myWebResponse.ResponseUri))
	Console.WriteLine("\nRequest Url : {0} was not redirected",url);   
else
	Console.WriteLine("\nRequest Url : {0} was redirected to {1}",url,myWebResponse.ResponseUri);   
// Release resources of response object.
myWebResponse.Close();

and then i will be connected ?

You don’t need the part

// Use "ResponseUri" property to get the actual Uri from where the response was attained.
if (ourUri.Equals(myWebResponse.ResponseUri))
   Console.WriteLine("\nRequest Url : {0} was not redirected",url);   
else
   Console.WriteLine("\nRequest Url : {0} was redirected to {1}",url,myWebResponse.ResponseUri);

but yes.

Or rather, instead of checking whether you’re redirected, you may want to check the actual response (I don’t know, status, contents, the actual value of ResponseUri… whatever) to see if the login was successful or if maybe your credentials were wrong or something, and then if there’s a problem, fail gracefully. You don’t have to of course, it’s just that the app would behave seemingly weird if the login fails for whatever reason.

So if I want to see the response
that why I will know that I login OK , and I’m now in “Active”

what should I leave and save?
so I will be able to show on my APP - connected , not connected?

Thanks,

You don’t need to “save” anything. You only need to “check” it, and act accordingly.

Do some requests to the router with a valid and invalid password. See if ResponseUri is different for example… I haven’t tried programmatically checking hotspot success to tell you if that’s what you need to check for, but that’s one possible start. See the HttpWebResponse class for all possible things you could check, although keep in mind that to get those, you need to use the HTTP specific variants, i.e.

HttpWebRequest myWebRequest = WebRequest.CreateHttp(url); 

// Send the 'HttpWebRequest' and wait for response.
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

Ok ,
I have a small problem now
this is waht I have :

string IP "192.168.100.254";
private void button2_Click_1(object sender, EventArgs e)
        {
                // Create a request for the URL.   
                WebRequest request = WebRequest.Create(
                  "http://192.168.100.254/login?username=david&password=david");
                // Get the response.  
                WebResponse response = request.GetResponse();
                textBox1.Text = (((HttpWebResponse)response).StatusDescription);

                textBox2.ForeColor = System.Drawing.Color.Green;
                textBox2.Text = ("You connected using WiFi hotspot!");

                response.Close();
            }
     
            mikrotik = new MK(IP);
            if (!mikrotik.Login("admin", password))
            {
                textBox2.ForeColor = System.Drawing.Color.Red;
                textBox2.Text = ("Could not log in - Wrong Password!");
                mikrotik.Close();
                return;
            }
             textBox2.ForeColor = System.Drawing.Color.Green;
            textBox2.Text = ("You have enter the router!");
        }

when I press the button I get connected - Ican see “OK” on the status , and also see I’m Active
but then it get stuck , and give me error
and in the router log I don’t even see a try to connected by API

so what is wrong?

Thanks ,

The line

mikrotik = new MK(IP);

(and everything related to the API) must be executed only after the request has been successfully completed, not before.

It seems like you’ve omitted the part that shows inside what method that line is in, but it clearly isn’t inside the click… If it’s inside the form’s constructor or init event, then it would be executed before the button is clicked, and therefore, before the HTTP request is made.

OK
So I made it work in 2 steps
first button - connect using hotspot
second button - get API data

it’s working good

my question -can I make it work by pressing only 1 button?
can I do this ?

Thanks ,

I mean, one obvious solution is to just move the 1st button’s logic into the second button’s logic, above the API related code.

If you want to only make an attempt to connect to the hotspot when that is necessary, then one way is to surround the line

mikrotik = new MK(IP);

in a try…catch block. That’s assuming that API client would throw an exception when failing to connect.

In the catch block, you’d do the hotspot HTTP request, and attempt connecting again before giving up.

Or if the API client isn’t throwing an exception… well… just use whatever means it has to let you check if the connection is successful, and make the HTTP request on failure.