-1

I've seen some similar issues, but nothing here seems to match my problem exactly.

I'm running a version of my API (C#) locally to test some updates. Its hooked up to a local SQL database.

When I run a query to get a token, postman works as expected. However, when I boot up my android emulator (xamarin) and run the same query, the requests times out with error code 0 almost instantly. I've set it to not time out at all. Another strange thing is that when I set the breakpoints in the local API, postman triggers those breakpoints, but the android app doesn't.

This is odd because both postman and the android app can query the live version of the API in the servers.

I've heard this might be a CORS issue?

Steps to reproduce the issue:

  1. Start up the local API which opens up a webpage to a local port.
  2. Test the port is working by sending a request with postman. The request I tried was requesting a token.
  3. Use postman to generate restsharp code, and run that code from inside the xamarin android project.

The error I get is always status code 0.

Code:

The following code was generated via postman after a successful request.

var client = new RestClient("http://localhost:53884/token");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Cookie", ".AspNet.Cookies=7u9...JRrb");
request.AddParameter("username", "redacted");
request.AddParameter("password", "redacted2");
request.AddParameter("grant_type", "password");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

I can't share the API code due to IP, but it works for the app when running on the servers, and both locally and live for postman.

pseudoabdul
  • 616
  • 3
  • 12
  • 2
    "Assuming" you are using `localhost` in your Android app to connect to your locally hosted service? i.e. `localhost` on the emulator is the emulator itself not the PC hosting the emulator – SushiHangover Nov 24 '20 at 02:17
  • "something works and something else does not... figure out what my problem is" puzzles are not really on-topic on SO. You need to [edit] the question with details that let one to understand and ideally preproduce the problem. "Run a query to get a token" is not really a concrete description of a problem - indeed narrowing down from all possible problems to *just* all possible problems with HTTP requests is a good step toward explanation of concrete problem to get it answered. – Alexei Levenkov Nov 24 '20 at 02:27
  • @SushiHangover I hadn't thought of that. That would explain why the request never reaches the API. I'll have a look into that and report back. – pseudoabdul Nov 24 '20 at 02:40
  • 2
    there are hundreds, if not more, questions dealing with problems using localhost to query services from a mobile app. Almost universally the answer is "use the IP or FQDN" – Jason Nov 24 '20 at 02:51
  • 1
    Does this answer your question? [How to connect to my http://localhost web server from Android Emulator](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator) – Cheesebaron Nov 24 '20 at 15:05
  • Thanks everyone. I hasn't considered that the local host for the device would be different. I've followed a few guides, but doing HTTPS requests with xamarin seems to have a few extra layers. If I figure it out I'll post my answer specific to xamarin/android. – pseudoabdul Nov 25 '20 at 01:15

1 Answers1

0

I have solved my issue, so I'll relay the steps I took.

This is assuming that you are running an API locally, and that you want be able to access the localhost with your android emulator. For the record, my android emulator is running my xamarin app, but it should work for any app.

  1. Install Conveyer (I followed these instructions)
  2. Close Visual studio.
  3. Allow Conveyer to install
  4. Open Visual Studio again
  5. When prompted about the firewall, let Conveyer automatically set your posts
  6. Open your API solution
  7. Right click on your API project and select "Set as start up project"
  8. The text next to the green run arrow at the top should change. Mine changed to "IIS Express (Microsoft Edge)". I believe you can change this in the web config file.
  9. Press the green arrow. This should do a few things. Firstly, it should open up a browser at a localhost address. For me it was http://localhost:53884/ . Don't be concerned if you get an error like "Server Error in '/' Application." Although I assume if you had the exact same problem as me, yours also worked in postman, so you've gotten this far.
  10. At the bottom of visual studio the will be a tab called "Conveyer by Keyoti". Click on it to reveal the URLs. Copy the http Remote URL address.
  11. Now open your xamarin app and wherever you were setting your API address (where you had local host), replace that address with the Remove URL address.
  12. Run your xamarin app, it should be able to access the API now.

I will say, I haven't got the https Remove URL working yet. I get the error: Error: SecureChannelFailure (Authentication failed because the remote party has closed the transport stream.) . My API allows both HTTP and HTTPS requests, so HTTP works for me, but if I get HTTPS working, I'll add an edit with how I did it.

pseudoabdul
  • 616
  • 3
  • 12