1

I've got an Asp.Net WebAPI running on my localhost, I can access to it from my computer using PostMan chrome extension, I can also connect to other applications hosted on my localhost from the Android Emulator but when I try to connect to the webApi with httpClient execute method it just returns a bad request html as result here is my Code..

ArrayList<NameValuePair> authParams = new ArrayList<NameValuePair>();
authParams.add(new BasicNameValuePair("cuentaAlumno", txtCuenta.getText().toString().trim()));
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://10.0.2.2:5289/api/values/");
httpPost.setHeader("Content-Type", "application/json");
//httpPost.setEntity(new UrlEncodedFormEntity(authParams));
HttpResponse httpResponse = httpClient.execute(httpPost);

I tried accesing localhost from Emulator explorer and it's ok It shows the IIS home page, also it's working for other web Apps the problem is just with the Api

David Robles
  • 86
  • 1
  • 8
  • Using localhost apparently works, but can your PC access the service on the same url, `http://10.0.2.2:5289/api/values/`? – Joachim Isaksson Apr 02 '14 at 17:33
  • 1
    Yes I can access the service from my computer but obviusly changing the ip with localhost/api/values.. – David Robles Apr 02 '14 at 17:44
  • Why change the IP? Does it not work from your local machine? – Joachim Isaksson Apr 02 '14 at 17:50
  • Thanks Joachim, Yes it works but 10.0.2.2 is not a valid IP for windows, I have to change it to localhost in order to make it work in windows.. – David Robles Apr 02 '14 at 17:56
  • Sounds like you're connecting to it, but the content of your request is incorrectly formatted. Are you missing some headers, or is the body of the request not what the .NET API expects? Try using Fiddler/Charles to inspect the traffic going to it. – Adam S Apr 02 '14 at 18:13
  • I've editted the code, what paramaters should I add to the headers ? – David Robles Apr 02 '14 at 22:21
  • That would likely depend on what the server requires. Can you hit the server with a known good client, and then with yours, and compare the server logs? – Chris Stratton Apr 03 '14 at 01:23
  • Hi I solved the problem with a solution I found in this post http://stackoverflow.com/questions/17719496/bad-request-when-trying-to-connect-to-iss-7-express-from-android-emulator I changed the IIS for the default ASP.NET web development server and now I can access to the Api from the emulator fine.. Thank you all. – David Robles Apr 03 '14 at 14:57

0 Answers0