简体   繁体   中英

Postman can query lcoal API, identical request send from android emulator can't

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM