So I've uploaded the following question a couple of times and didn't really get a tutorial-kind answer. Here's the way I've found to solve this problem:
I need to access my asmx web service from a different device in the LAN (an Android device), but it is running on Visual Studio's localhost. How do I send HTTP messages to a web service that's on a different computer?
So what we need to do is as following:
1) Create your ASP empty web application, add an ASMX web service. Go to the properties (Web tab) of the project , and choose the web service as the start action (in order to prevent it from trying to open the config file). In the Project URL line change the port as you wish and create the virtual directory:
2) Now on my project I wanted to add support for both POST and GET messages. In order to do that, go to the web.config file and add the following code inside the <configuration>
tag:
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
3) The third thing we want to do is bind the localhost with the computer's actual IP address. My laptop has a constant IP (192.168.1.10), which makes programming the application simpler, but any IP could work. Go to your documents folder, and then edit IISExpress/config/applicationhost.config. Find the <sites>
tag, and inside it your project's name. Change the <bindings>
tag so that it looks like this:
<bindings>
<binding protocol="http" bindingInformation="*:4250:192.168.1.10" />
<binding protocol="http" bindingInformation="*:4250:localhost" />
</bindings>
Now instead of 192.168.1.10 you could just write *, but again in the android app you want to just know the IP address instead of setting it with an EditText etc. After this stage, you can test the webservice by typing http://YOUR_IP:YOUR_PORT/YOUR_WEB_SERVICE_NAME.asmx/FUNCTION and making sure it returns a valid value.
4) Now in order to stop the computer's firewall from dropping the messages, you can either create an exception (inside in the selected port) or just turn off the firewall. Without that, your android app will get stuck and wait for timeout in the execution process of the HTTP message (which takes long to debug because it doesn't throw an exception).
5) Build your android app: I'm not going to bring all of the code, but there is a button that calls an AsyncTask in order to send an HTTP GET message. Important thing is to add permissions to the app:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
The code inside my AsyncTask class is as following:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://192.168.1.10:4250/WebService1.asmx/HelloWorld");
HttpResponse httpresponse = client.execute(get);
HttpEntity httpEntity = httpresponse.getEntity();
if (httpresponse.getStatusLine().getStatusCode() == 200) {
InputStream is = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
String text;
while ((text = bufferedReader.readLine()) != null) {
response += text;
}
What it does is basically take the returned string (from the HelloWorld function) and save it to the local variable (response).
That's all! Hope it's all working for you.