Assuming a legitimate scenario where both networks A and B are equally valid for raw internet access (say, a device with both a wifi and wired connection that are weighted equally), you can control this using routes.
Routes are not chosen by code in your application. Rather, they are part of the network configuration on the computer. You may feel tempted to use application code to alter the network configuration, but this is almost always a very bad idea.
To set the correct route in your network configuration, you need to know the IP address of your service (this kinda sucks, since often you only start out with a host name, and the IP address might even be dynamic in the case of cloud services). You must also know the network address for connection A. The network address is different from the gateway IP address, and typically ends with a 0
(though it is possible to construct network subnets with different network addresses).
Once you add the route, connections on the computer targeting your service's address will see this new route is more specific than the default gateways at A or B, and therefore always choose it.
A common use for routes is forcing certain traffic to pass over a special connection, such as a VPN.