I'm running a web service on my local machine that runs at localhost:54722.
I want to call the service from an app running in the Android emulator.
I read that using 10.0.2.2 in the app would access localhost, but it doesn't seem to work with the port number as well. It says HttpResponseException: Bad Request.
You can access your host machine with the IP address "10.0.2.2".
This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:<hostport>".
If your emulator must access the internet through a proxy server, you can configure a custom HTTP proxy from the emulator's Extended controls screen. With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.
Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion
Since 10.0.2.2 is not a secure domain for Android you have to allow non-secured domains in your network configuration for API 28+ where non-TLS connections are prevented by default.
You may use my following configurations:
Create a new file in main/res/xml/network_security_config.xml as:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
And point it in AndroidManifest.xml
<application
......
......
android:networkSecurityConfig="#xml/network_security_config">
I faced the same issue on Visual Studio executing an web app on IIS Express. to fix it you need to go to your project properties then click on Debug Tab and change http://localhost:[YOUR PORT] to http://127.0.0.1:[YOUR PORT] and set the android url to http://10.0.2.2:[YOUR PORT]. it worked for me.
I'm not sure this solution will work for every Android Emulator and every Android SDK version out there but running the following did the trick for me.
adb reverse tcp:54722 tcp:54722
You'll need to have your emulator up an running and then you'll be able to hit localhost:54722 inside the running emulator device successfully.
If you are using IIS Express you may need to bind to all hostnames instead of just `localhost'. Check this fine answer:
https://stackoverflow.com/a/15809698/383761
Tell IIS Express itself to bind to all ip addresses and hostnames. In your .config file (typically %userprofile%\My
Documents\IISExpress\config\applicationhost.config, or
$(solutionDir).vs\config\applicationhost.config for Visual Studio
2015), find your site's binding element, and add
<binding protocol="http" bindingInformation="*:8080:*" />
Make sure to add it as a second binding instead of modifying the existing one or VS will just re-add a new site appended with a (1) Also, you may need to run VS as an administrator.
I solved it with the installation of "Conveyor by Keyoti" in Visual Studio Professional 2015.
Conveyor generate a REMOTE address (your IP) with a port (45455) that enable external request.
Example:
Conveyor allows you test web applications from from external tablets and phones on your network or from Android emulators (without http://10.0.2.2:<hostport>)
The steps are in the following link :
https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti
The problem is that the Android emulator maps 10.0.2.2 to 127.0.0.1, not to localhost. So configure your web server to serveron 127.0.0.1:54722 and not localhost:54722. That should do it.
After running your local host you get http://localhost:[port number]/ here you found your port number.
Then get your IP address from Command, Open your windows command and type ipconfig
In my case, IP was 192.168.10.33 so my URL will be http://192.168.10.33:[port number]/.
In Android, the studio uses this URL as your URL. And after that set your URL and your port number in manual proxy for the emulator.
I have a webserver running on my localhost.
If I open up the emulator and want to connect to my localhost I am using 192.168.x.x. This means you should use your local lan ip address. By the way, your HttpResponseException (Bad Request) doesn't mean that the host is not reachable.
Some other errors lead to this exception.
To access localhost on Android Emulator
Add the internet permission from AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Add android:usesCleartextTraffic="true", more details here:
Run the below-mentioned command to find your system IP address:
ifconfig | grep "inet " | grep -v 127.0.0.1
Copy the IP address obtained from this step (A)
Run your backend application, which you can access at localhost or 127.0.0.1 from your sytem.
Now in android studio, you can replace the URL if you're using in code or You can use the ip address obtained from step(A) and try opening in web browser,
Like this http://192.168.0.102:8080/
Don't forget to add PORT after the IP address, in my case app was running on 8080 port so I added IP obtained in (A) with the port 8080
you need to set URL as 10.0.2.2:portNr
portNr = the given port by ASP.NET Development Server my current service is running on localhost:3229/Service.svc
so my url is 10.0.2.2:3229
i'd fixed my problem this way
i hope it helps...
"BadRequest" is an error which usually got send by the server itself, see rfc 2616
10.4.1 400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
So you got a working connection to the server, but your request doesn't fit the expecet form. I don't know how you create the connection, what headers are included (if there are any) – but thats what you should checking for.
If you need more help about, explain what your code is about and what it uses to connect to the Server, so we have the big picture.
Here is a question with the same Problem – the answer was that the content-type wasnt set in the header.
1) Run ipconfig command in cmd
2) You will get result like this
3) Then use IPv4 Address of VMWare Network Adapter 1 followed by port number
In My Case its 8080, so instead of using localhost:8080
I am using 192.168.56.1:8080
Done.....
I would like to show you the way I access IISExpress Web APIs from my Android Emulator. I'm using Visual Studio 2015. And I call the Android Emulator from Android Studio.
All of what I need to do is adding the following line to the binding configuration in my applicationhost.config file
<binding protocol="http" bindingInformation="*:<your-port>:" />
Then I check and use the IP4 Address to access my API from Android emulator
Requirement: you must run Visual Studio as Administrator. This post gives a perfect way to do this.
For more details, please visit my post on github
Hope this helps.
For Laravel Homestead Users:
If anyone using Laravel with homestead you can access app backend using 192.168.10.10 in emulator
Still not working?
Another good solution is to use ngrok https://ngrok.com/
I am using Windows 10 as my development platform, accessing 10.0.2.2:port in my emulator is not working as expected, and the same result for other solutions in this question as well.
After several hours of digging, I found that if you add -writable-system argument to the emulator startup command, things will just work.
You have to start an emulator via command line like below:
emulator.exe -avd <emulator_name> -writable-system
Then in your emulator, you can access your API service running on host machine, using LAN IP address and binding port:
http://192.168.1.2:<port>
Hope this helps you out.
About start emulator from command line: https://developer.android.com/studio/run/emulator-commandline.
Explanation why localhost is not available from emulators for anyone who has basic access problem. For sophisticated cases read other answers.
Problem: Emulator has own local network and localhost maps itself to emulator, but NOT your host!
Solution:
Bind your server to 0.0.0.0 to make it available for emulator's network
Get external IP address of your laptop: ifconfig command for Mac
In Android (or Flutter app) use IP address of your external interface like: 192.168.1.10 instead of localhost
I had the same issue when I was trying to connect to my IIS .NET Webservice from the Android emulator.
install npm install -g iisexpress-proxy
iisexpress-proxy 53990 to 9000 to proxy IIS express port to 9000 and access port 9000 from emulator like "http://10.0.2.2:9000"
the reason seems to be by default, IIS Express doesn't allow connections from network
https://forums.asp.net/t/2125232.aspx?Bad+Request+Invalid+Hostname+when+accessing+localhost+Web+API+or+Web+App+from+across+LAN
localhost seemed to be working fine in my emulator at start and then i started getting connection refused exception
i used 127.0.2.2 from the emulator browser and it worked and when i used this in my android app in emulator it again started showing the connection refused problem.
then i did ifconfig and i used the ip 192.168.2.2 and it worked perfectly
Bad request generally means the format of the data you are sending is incorrect. May be mismatched data mapping . If you are getting bad request implies you are able to connect to the server, but the request is not being sent properly.
If anybody is still looking for this, this is how it worked for me.
You need to find the IP of your machine with respect to the device/emulator you are connected. For Emulators on of the way is by following below steps;
Go to VM Virtual box -> select connected device in the list.
Select Settings ->Network-> Find out to which network the device is attached. For me it was 'VirtualBox Host-Only Ethernet Adapter #2'.
In virtualbox go to Files->Preferences->Network->Host-Only Networks, and find out the IPv4 for the network specified in above step. (By Hovering you will get the info)
Provide this IP to access the localhost from emulator. The Port is same as you have provided while running/publishing your services.
Note #1 : Make sure you have taken care of firewalls and inbound rules.
Note #2 : Please check this IP after you restart your machine. For some reason, even If I provided "Use the following IP" The Host-Only IP got changed.
I resolved exact the problem when the service layer is using Visual Studio IIS Express. Just point to 10.0.2.2:port wont work. Instead of messing around the IIS Express as mentioned by other posts, I just put a proxy in front of the IIS Express. For example, apache or nginx. The nginx.conf will look like
# Mobile API
server {
listen 8090;
server_name default_server;
location / {
proxy_pass http://localhost:54722;
}
}
Then the android needs to points to my IP address as 192.168.x.x:8090
if you are using some 3rd party package like node express or angular-cli you will need to find the IP of your machine, and attach your host to that IP within the server startup config (instead of localhost). Then launch it from the emulator using the IP. For example, I had to use: ng serve -H 10.149.212.104 to use the angular-cli. Then from the emulator I used: http://10.149.212.104:4200
If you are working with Asp.Net Web API, in .vs/config folder inside your project, modify these lines as per you port setting. Let suppose you use port 1234 and physicalPath to the project folder set by IIS is "D:\My Projects\YourSiteName", then
<site name="YourSiteName" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="D:\My Projects\YourSiteName" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:1234:*" />
</bindings>
</site>
In android studio, access your api with "http://10.0.2.2:1234" ...
Using the OS X server application, I can point multiple domains to various websites. All domains can use port 80 at the same time and OS X will resolve which website to use.
When I start my kitura server (I suppose the same happens with Vapor and Perfect) I have to start it on a free port (ex. 8080). if I start it on 80 it will create a conflict with the websites and it will probably not answer my calls.
What is the correct way to setup Kitura so that port 80 is used together with other services?
I start kitura server using the following:
do {
let controller = try Controller()
Log.info("Server will be started on '\(controller.url)'.")
Kitura.addHTTPServer(onPort: controller.port, with: controller.router)
// Start Kitura-Starter server
Kitura.run()
} catch let error {
Log.error(error.localizedDescription)
Log.error("Oops... something went wrong. Server did not start!")
}
And I get the following on the log:
[2017-06-27T17:57:30.635+03:00] [VERBOSE] [Router.swift:68 init(mergeParameters:)] Router initialized
[2017-06-27T17:57:36.817+03:00] [INFO] [main.swift:37 KiteSpotterServer] Server will be started on 'http://localhost:8080'.
[2017-06-27T17:57:37.588+03:00] [VERBOSE] [Kitura.swift:72 run()] Starting Kitura framework...
[2017-06-27T17:57:38.457+03:00] [VERBOSE] [Kitura.swift:82 start()] Starting an HTTP Server on port 8080...
[2017-06-27T17:57:39.190+03:00] [INFO] [HTTPServer.swift:117 listen(on:)] Listening on port 8080
The most common and probably easiest way to make your application reachable via port 80 on OS X (or macOS), is to configure a reverse proxy.
This solution applies to any web application, not just Kitura.
The OS X Server app comes with an Apache server which can be configured to act as a reverse proxy. The Apache reverse proxy can listen on port 80 for a specific domain, then forward all requests to the internal port 8080 your application listens on, and vice versa.
How to do this properly:
Step 1: In /Library/Server/Web/config/apache2/webapps, create a plist file called my.application.plist (for example). This plist describes a so-called webapp which can than be activated in the OS X server app.
Example webapp plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- Allow to include a custom Apache configuration file from Server app -->
<plist version="1.0">
<dict>
<key>includeFiles</key>
<array>
<!-- Include files are activated in virtual host when webapp is started -->
<string>/Library/Server/Web/Config/apache2/httpd_webapp_myapp.conf</string>
</array>
<key>name</key>
<string>com.atlassian.webapp.myapp</string>
<key>displayName</key> <!-- Name shown in Server app -->
<string>Reverse proxy for myapp</string>
<key>installationIndicatorFilePath</key> <!-- The presence of this file indicates web app is installed -->
<string>/Library/Server/Web/Config/apache2/httpd_webapp_myapp.conf</string>
<key>sslPolicy</key><!-- Determines webapp SSL behavior -->
<integer>0</integer>
<!-- 0: default, UseSSLWhenEnabled -->
<!-- 1: UseSSLAlways -->
<!-- 2: UseSSLOnlyWhenCertificateIsTrustable -->
<!-- 3: UseSSLNever -->
<!-- 4: UseSSLAndNonSSL -->
</dict>
</plist>
In the above example, replace myapp by your app name.
Step 2: Next, in /Library/Server/Web/Config/apache2, create a file with the name you selected in the plist file (httpd_webapp_myapp.conf in my example). That file will configure the reverse proxy.
Example proxy configuration file:
# As described at http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
# Service proxy for myapp installation
ProxyRequests Off
ProxyVia Off
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://myapp.mydomain:8080/
ProxyPassReverse / http://myapp.mydomain:8080/
In the above example, replace myapp.mydomain by the domain under which you want to reach your application (and 8080 by the application's port, if it's different).
Important: Both of these files must be created/edited using sudo nano, and they must be owned by root.
Step 3: In the OS X Server app, go to Websites, select the domain you want to use (double-click or click the edit symbol), then click on Edit advanced settings. If all went well, your newly created webapp will appear under Make these webapps available under this website. Check the box of your webapp, then save.
It's usually not necessary to restart the web server (the Server app will do this for you).
Step 4: Test if you can reach your application under this domain.
Note: It may be necessary to configure the application somehow, so it knows it runs behind a reverse proxy.
At first glance, this may appear a bit complicated, but it isn't really, and as far as I know, it is now the only accepted way to configure a reverse proxy on OS X server.
OSX Server is already using port 80 to listen to incoming connections. Therefore Kitura cannot use port 80 while OSX Server is using it. To use Kitura on port 80 you would need to shut down OSX Server to free up port 80. Alternatively you can set up OSX Server to route requests to your website to your Kitura application. But I do not know how to do this with OSX Server specifically.
I am building a website, and I would like to see how it is rendered on an Android Smartphone, so I downloaded Genymotion. I can't see any pages on my local site from Genymotion ("Bad request - invalid hostname").
When I launch the Visual Studio solution, the homepage address is
http://localhost:18207
so following the advice that I found for example here I typed in Genymotion the following addresses:
http://10.0.3.2:18207
http://192.168.56.1:18207
http://(my ip address):18207
but I always have the above mentioned error, or sometimes a timeout error.
Thanks a lot for any suggestion!
I ran into this exact same issue and resovled it with the help of this blog post:
http://blog.binarybits.net/applications/iis-express-http-error-400-the-request-hostname-is-invalid/
I don't want to take any credit for the content of that post, but just in case it goes offline I'll lay out the process. Essentially you need to update the configuration for IIS Express so it'll accept the incoming request from Genymotion. So assuming that your site is running on port 8080:
Step 1 - Modify the specific configuration for your site in C:\Users\\Documents\IISExpress\config\applicationhost.config so it's tied to * instead of localhost
<site name="WebSite1" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="PATH TO YOUR SITE" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":8080:*" /> <!-- CHANGE THIS LINE -->
</bindings>
</site>
Step 2 - Add an ACL rule to let incoming connections access your laptop on this (run this command via admin)
netsh http add urlacl url=http://*:8080/ user=everyone
Step 3 - Finally connect to your site within Genymotion using this special address
http://10.0.3.2:8080/
That worked for me, so hopefully it works for you as well.
In my case, a slight modification in the answer provided by Sam Storie solved the problem.
My local service is hosted using MVC Web Api controllers (I'm using Visual Studio 2013).
In step 1, instead of replacing :8080:localhost to :8080:*, I just added a new binding right next to the existing one. The new binding does not have either localhost or * in it. It will look like this:
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" /> <!-- Existing binding -->
<binding protocol="http" bindingInformation=":8080:" /> <!-- New binding. Note that there is no asterisk(*)-->
</bindings>
Just in case it is not clear, ensure that you replace 8080 with the actual port where the service is hosted on localhost. In my case it is 53533.
Run your web server on the 192.168.56.1 net. This is usually a setting in the server config. It's probably defaulting to the real adapter of your host, not the virtual adapter that VirtualBox is connected to. I'm not sure what host OS you're on, but the virtual adapter is usually vbox0 for a Linux install. This is the virtual network that Genymotion is on.
Your Genymotion VM will have a DHCP address like 192.168.56.101. You can check for sure with the Genymotion config app inside the VM. If this is the case, and your web server is also on this subnet, you should be able to point your Genymotion browser to 192.168.56.1.
You just need to use http://yourHostIpAdress/YourWebSite (no need of port)
At least this worked when publishing the website locally through IIS
Normally, I configured my Tomcat 7 to perform redirect from port 8080 to 8443. Below is the portion of the configuration and everything works as expected.
server.xml
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" />
<!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the
JSSE configuration, when using APR, the connector should be using the OpenSSL
style configuration described in the APR documentation -->
<Connector SSLEnabled="true" clientAuth="false"
keystoreFile="conf/somestore" keystorePass="somekey"
maxThreads="200" port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
scheme="https" secure="true" sslProtocol="TLS" />
Only today, I decided to removed the first connector from configuration. However after I hit start the server button in Eclipse everything seems fine except the message says Starting Tomcat 7 never ends.
and eventually this:
By the way, this issue only appears in Tomcat within Eclipse. Is it possible that Eclipse is still trying to access the application through the old port ? Any pointers is much appreciated.
UPDATED
After I double clicked the server instance, I am only able to see two ports number under Ports section.
Tomcat Admin Port which is 8005
SSL Port which is 8443
I had the same issue. Originally I had both a secure and non-secure port open and when I removed the non-secure connector, I had the same issue with Eclipse saying it failed to start.
I resolved the issue by putting a non-secure connector back in.
I am guessing Eclipse tries to access the application to verify it is running and doesn't handle secure ports very well.
Just goto the tomcat configuration console by double clicking on tomcat under the servers .
Check the port specified there .
If it doesn't help , delete the server and install the tomcat again in eclipse. It only takes a matter of seconds .
Also please try exiting all the tomcat processes from windows and then try to start the tomcat from eclipse once again .
Eclipse makes a copy of the server configuration and saves it under the "Servers" project.
In the Server View, double clic your server name and when configuration window opens you can edit under "Ports" section.
Edited***
You can go further by opening the "Servers" project, then editing server.xml directly looking for the "Connector" tag that holds the current 8080 port.
http://farm8.staticflickr.com/7390/9559799161_3a152c1ac1_o.jpg
I have a problem. I am trying to run a simple JSP page from Eclipse:
http://postimg.org/image/z268cl1s3/
But when I run this page i get a 404 error:
http://postimg.org/image/h0rosix4z/
When I put in:
localhost
127.0.0.1
in the browser it works fine:
http://postimg.org/image/8js6hlsg3/
I can see eclipse is running it from localhost:8080, but when i type that in at the browser it gives me this error:
http://postimg.org/image/7lbtfbf43/
Does anyone know how I can activate localhost on my mac. I looked up several tutorials, but i didnt find the answer. MySQL is also running fine on the computer, so I guess there is something that dont let me access [http://localhost:8080?]
Hope someone can help me?
Best Regards
Mads
There are two issues you are having:
There is a difference between http://localhost/ (and its equivalent http://127.0.0.1) and http://localhost:8080 - the first uses port 80, the second 8080. The Tomcat server listens only to the latter, that is shown by the servers error messages, and not generic 404 messages. In other words, do not forget to add the 8080 port numbers to the end of the localhost url
The Tomcat error messages show that the resources are not available, so I would look at what Java web applications are installed - e.g. the root web application is missing (the localhost:8080 url) for sure, and I am not sure whether your MySQL connector is.
Search google for how adding an entry to the hosts file on your mac.
You'll basically link localhost to 127.0.0.1 there