I am developing a tablet application which needs to connect to a web site to collect online content.
I have seen that we can indeed connect to a web server on a local system by addressing it via it's IP address.
However, I am using virtual hosts on my system, so as to use virtual domains.
This is setup on my system in the apache httpd-vhosts.conf file like this -
#
# Project wordpress dev site
#
<VirtualHost *:80>
DocumentRoot "C:/web/www/boutique"
ServerName boutique.project.dev
</VirtualHost>
with my hosts file having the following entry
127.0.0.1 boutique.project.dev # project woocommerce site
I am using the HttpPost and HttpClient classes and I cannot see how I can provide the real IP address whilst still transmitting the host name in the URL request.
So, what I need to know is how can I make queries from my application using the virtual address " boutique.project.dev " ?
Is this possible ?
Edit :
Following one comment, I need to make things more clearer.
I am wanting to do this in code. Each time we make a connection to a site, the URL does a DNS lookup to determine the IP address to use. I need to intercept this and provide the IP address of my local system. I have seen some examples for proxy's using HttpHost, but I am unclear as to how to use this or even if it is relevant.
I think you don't do that unless you modify host file in android device (with root permissions)
This is a similar question: Apache Virtual Host (Subdomain) access with different computer on LAN
You also could setup a DNS server in your computer (i.e bind9) and set entries for your apache virtualhost and configure android in order to use that DNS server.
Here is the solution which I hope will avoid some useless "banging the head against a wall" for those of you who might have the same need.
Two or three steps are necessary.
1st step : Open the firewall to allow connections.
This is obviously necessary or else, whatever you do the connection will not be made. I use Comodo and so I added a rule in the firewall permitting all connections coming from the local LAN.
2nd step : Tell your server, apache in my case, to listen on its IP address.
I added the following entry in my httpd.conf file :
Listen [my ip address]:80
3rd step : Code the connection
The key to getting this to work is to tell which virtual server is needed. So, without further boring details, here is the code :
// setup the needed objects
HttpClient client = new DefaultHttpClient();
// create the request using the IP address of the server machine
HttpGet request = new HttpGet("http://[target ip address]:80/testpage.php");
// here is the code magic - manually set the host header
request.setHeader(HttpHeaders.HOST, "boutique.project.dev");
// now execute the request
HttpResponse response = client.execute(request);
// read back the response as normal
....
The result is that the apache server will map the request to the virtual host.
I hope this will be useful for others here !
Oh, I almost forgot, don't forget to enable the WiFi on your tablet - can save hours of wondering why it doesn't work !
Related
So I set up a few virtual hosts with unique urls and they work just fine on the desktop. However, when I connect a mobile device on the network, it can't seem to access anything properly but the default localhost virtualhost and that's only when it's the only virtualhost I have up.
My setup and coding is pretty much this except with a different site title
wamp server 3.0 virtual host on another device
and while that solution redirects me to my unique url, it has a lack of images on a default wordpress website.
Has anyone managed to get mobile devices fully accessing links other than on localhost?
Since I posted the answer you referenced, I have decided upon a simpler solution.
What the actual problem is
Because we cannot fiddle with the configuration of a phone like we can with a PC, the phone can never find the domain name we create in our Virtual Host definition on the Server machine, because it does not exist in any DNS Server for it to locate the IP Address in, and a DNS Server is the only place a phone can look, unless it is jail broke.
If you wanted to access one of your Virtual Hosts domains from another PC you could just add a line like this into the HOSTS file on the other PC like this.
192.168.0.10 example.local
But you cannot do that on a phone/tablet.
What Apache expects to be able to asssociate a request to a Vhost
When we create an Apache Virtual Host, we are actually telling Apache to look at the domain name on the incoming connection and match that domain name to a ServerName that exists in one of our multiple Virtual Hosts definitions.
But if we use for example example.local as our virtually hosted domain when we attempt to connect to that from our phone, the phone does a DNS Lookup and does not find that domain and therefore cannot get its ip address.
The simplest way to get round this is:
Assuming we do not have access to adding record to a DNS Server we have to come up with a different solution.
The simplest of these is to use the IP Address of the PC running the WAMPServer(Apache) server and a specific port number. So thats a different port number for each of our sites we want to use from a phone.
So how do we do this
Add the new listening port to httpd.conf like so after the 2 existing Listen statements
WAMPServer 3: Do this using the menus, not by doing a manual edit on httpd.conf
right click wampmanager-> Tools -> Add listen port for Apache
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen [::0]:80
Listen 0.0.0.0:8000
Listen [::0]:8000
Suggested httpd-vhosts.conf file
#
# Virtual Hosts
#
# Always keep localhost, and always first in the list
# this way a ramdom look at your IP address from an external IP
# maybe a hack, will get told access denied
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp/www
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# The normal Vhost definition for one of our sites
<VirtualHost *:80>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# Access example.dev from phone for testing
<VirtualHost *:8000>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
# assuming yoursubnet is 192.168.0.?
# allow any ip on your WIFI access
Require ip 192.168.0
</Directory>
</VirtualHost>
Restart Apache from wampmanager after completing these edits.
Now you test this from the WAMPServer PC by using the ServerName i.e example.dev and from the phone using the ip of the PC running WAMPServer with the port number i.e. 192.168.0.10:8000
Apache will find the correct code to serve from both requests.
If you want more than one Virtual Host to be accessible from your phone you just duplicate this idea and change the port number for each new site, lets say you would use 8001,8002,8003 etc. For as many sites as you want to access.
You may also have to amend your firewall to allow access on http on port 8000, or whatever port you pick to use
I have an ASP.net Web API in my laptop with this address :
localhost:99949
I add this to IIS Express that I can access my web Api from another computer in same lan network , and it's going this:
Nimis:80
I can access to my web api from other PCs , but when I try to access this with my android device it show me "Web page not available" error.
I turn off all my firewalls.
what should I do to fix it ?
You need to add an inbound rule in the firewall for port 80 (or whatever port you used for your website on IIS):
Go to Control Panel, Windows Firewall
Select Inbound Rules
Add a New Rule
Select "Port" as a Rule Type
Select "TCP and put "80" (and any other ports you want to open) in "Specific local ports"
Select "Allow the connection"
Select the network location where the rule should apply
Give a name and an optional description
After that you should be able to access your site from other devices in the same network using http://computername (e.g. http://myhomepc)
However you might need to use the IP of the server machine with Android. It always seems to override its DNS entries using Google's servers. In this case try to modify DNS settings as explained here.
I had the same problem and this is my solution without changing anything in FireWall or any other settings:
Get the IP of the WebService deployed by IIS-Express (run IIS-Express): For this install the extension "Conveyor" in Visual Studio: Tutorial on Youtube
To check if it is working open browser on your mobile and type in the IP shown in Conveyor: IP of WebService --> e.g. I typed URL: "http://192.168.178.51:45455/api/ToDo" to get the correct HttpGet from my Rest-Webservice
To have it running in Android Studio I used a normal HttpURLConnection (Same URL also runs on Emulator!)
it is my second time asking a question on stackoverflow. I'm trying to access to my local apache server that I've set up on my desktop computer (ip that starts with ie. 192.168.1*.**)on my android phone. but somehow it does not allow me to access to the server at all! :( strangely, sometimes it does allow me to access to the server lol! but it's only one out of 20 for each attempts. Also, it doesn't allow me to access to the server on my laptop either.. I really need some help guys.. it was working fine when i was working locally within my desktop pc by using emulator. I've done quite a lot of research about it but i wasn't able to find any solutions.
Thank you guys
=====================================
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient client = new DefaultHttpClient(setHttpParams());
HttpPost request = new HttpPost(PATH + FILENAME);
if (json != null) {
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
} else {
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse response = client.execute(request);
so that is the code, error occurs when it tries to execute the request. request includes the apache server ip PATH (192.168.**) + FILE_NAME(ie. index.php). I know why it causes the error because i cannot access to the 192.168.* server and read the desired file. I need help with this =(
For the computer running the webserver:
Make sure that your personal firewall rules are not preventing access to the web server.
For your phone to access the webserver over the air the server needs to be accessible from the web. It could be that your machine is behind a corporate firewall which is preventing access to it from external sources.
If you simply want to access it from the phone, make sure that both the webserver and phone are on the same network i.e. they have IP addresses from the same address range. You can do that by connecting your machine to wireless AP (either via ethernet or wireless) and making sure that your phone is connected to the same wireless AP.
Moreover I just read your question again and noticed the line
it does not allow access to to the server on my laptop either
If you cannot access the webserver locally on the machine it is runnning on then there is something wrong in the way you configured your web server.
So first make sure that your web server is running and configured properly. Open a browser and go to http://localhost or http://192.168.x.x if everything is configured right you will be able to access your web pages.
Basically this is networking 101, if it is still giving you problem post some details such as what webserver, what port etc.
If you are new to networking concepts and don't know how to configure a web server say Apache just Google how to configure Apache or use something called XAMPP which is a developer friendly bundle containing Apache, MySQL, PHP, FileZilla etc. Note that XAMPP shall never be used in a production environment as its default configuration is done in a way to make it easier to install, configure and run above mentioned packages for new users.
I am creating an aplication that involves an WebView. The thing is that I want to load the full page and not the mobile one, so I have changed the User Agent. Nevertheless there are pages that loads the mobile version.
Here are two versions of code that I have tried:
1.webview.getSettings().setUserAgentString("Mozila ");
2.
String DESKTOP_USERAGENT = webview.getSettings().getUserAgentString ();
DESKTOP_USERAGENT = DESKTOP_USERAGENT.replace("Mobile ","");
webview.getSettings().setUserAgentString(DESKTOP_USERAGENT);
This are exemples of webpages that loads the mobile version in any cases:
http://www.jurnalul.ro
http://www.androidzoom.com
1.Does anyone knows how I can trick the server and tell him I am using a desktop and not a mobile?
2.How does a website knows that I am using a mobile version?
Thank you very much,
Razvan
The problem may be that if you are using a device that your carrier is routing all your HTTP requests through a proxy, and that the proxy is changing the User-Agent. Check on the other end, with your own server, using nc -l 80 -vvv that your request is indeed sending the User-Agent that you have modified.
EDIT: Some specific troubleshooting steps
Forward a port 9090 on your router to your desktop machine or laptop.
Download netcat
Run netcat with the command "nc -l 9090 -vvv"
In your Android application's WebView, make an HTTP request with the User-Agent you are injecting to http://your.ip.address:9090
In the terminal you ran netcat, you will see the HTTP request dump in plain text. There you can check the HTTP header User-Agent to see if it has been changed by a proxy server or not.
You cannot test this stuff with Wireshark or Fiddler because it is happening in the WAN. You need to test it on the receiving end, either on a server, or on your own desktop machine.
webview.getSettings().setUserAgent(1);//for desktop 1 or mobil 0.
I wrote a little WS on asp.net, I can open it printing something like
http://46.146.170.225/RouteGen/Service.asmx
in address bar. It's all right, WS works.
But if I print the same address in a browser on the other computer, the page isn't available. How to get access to my web server from other PC? (I need from Android device, but I think it's no difference)
If you started the Web-Service from within Visual Studio then without changing the starup-settings of your project - it's not possible, because VS only starts a local debug web-server that doesn't allow calls from other hosts than localhost.
To allow external IPs to access your web-server, you have to set up the IIS and run your web-service inside it. A firewall could block incoming requests to the IIS but I ran such a service last winter and didn't have to change firewall-settings.
Verify that the website, in IIS, is bound to a public-facing IP address. Right click on your website in IIS, and go to the bindings setting. Then, check the host field. It should have an IP address or domain name that is available publicly.
Verify that your firewall has Port 80 open for incoming traffic