Reputation: 1532
Thanks for your attention.
I'm trying to make a Xamarin.Forms
App that communicates with a REST
API.
The API is fully functional, I've tested it with Postman and another WPF project.
Upon trying to call a simple GET
method with my HttpClient
, I get the following HttpResponseMessage
{
StatusCode: 400,
ReasonPhrase: 'Bad Request',
Version: 1.1,
Content: System.Net.Http.StreamContent,
Headers:
{
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 17 May 2020 12:20:37 GMT
Connection: close
Content-Type: text/html; charset=us-ascii
Content-Length: 334
}
}
public async Task TurnLEDOn()
{
HttpClient Client = new HttpClient(new System.Net.Http.HttpClientHandler());
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Json"));
Client.BaseAddress = new Uri("https://192.168.1.2:44341/");
HttpResponseMessage response = await Client.GetAsync("api/ChipCore/TurnLEDOn");
if (response.IsSuccessStatusCode)
{
string receivedPerson = await response.Content.ReadAsAsync<string>();
}
}
This same code is run by a WPF program and works flawlessly.
Both my phone and Laptop are connected to the same Wi-Fi network.
44341
through Firewall, I've even tried running
the whole thing with Firewall off. The browser on my phone can't
access the API. (Bad Request)https
to http
but faced
a trust anchor for certification path not found
I have added these Permissions to android.manifest
:CHANGE_NETWORK_STATE
CHANGE_WIFI_STATE
ACCESS_NETWORK_STATE
INTERNET
Method in API
[RoutePrefix("api/ChipCore")]
public class ValuesController : ApiController
{
[HttpGet]
[Route("TurnLEDOn")]
public bool TurnLEDOn()
{
return Switch.TurnLEDOn();
}
}
Update
I have tried bypassing the Certificate by adding this line to MainActivity.cs
ServicePointManager.ServerCertificateValidationCallback += (o, cert, chain,
errors) => true;
I have tired putting in the URL in my phones browser, I get Error 400 - Bad Request
Upvotes: 7
Views: 3888
Reputation: 5016
IIS Express cannot process requests from other machines.
You can:
Install the free extension I made called Conveyor which doesn't modify anything to do with your project or configuration https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti
Or, modify the configuration in IIS Express How to enable external request in IIS Express?
Or, host the project on IIS proper
Upvotes: 2
Reputation: 442
assuming that you are running IIS Express in debug mode, it won't allow any connection from outside your computer, I've had the same problem with a WebAPI before
you may wanna try to run your app in a VM or something you should also try accessing the API from another computer, you can test it using your ready WPF app, i believe you'll face the same problem then
you could also try to send postman requests using the Server IP address instead of localhost
If you happen to reproduce the problem this way then all you need is to configure the IIS to server remote/external traffic
Upvotes: 0
Reputation: 826
The problem looks like your phone is not connected to your server (which is hosted on your computer).
If you are able to access this same request using Postman, it shouldn't be a problem but let's find out.
Step 1) What do your server logs say? When your phone throws a 400 bad request error, did it really even connect to your computer? Just go to your server logs and find out whether was it even hit? If the server was hit, then go to step 2. Else go to step 3 for one more confirmation.
Step 2) Can you compare the request made through postman with the request made through your phone? Both the header and the body? Or with the WPF request? You need to find out what's the different because something definitely is.
Step 3) Can you try one more thing. Just create a new controller at root "/" and send a "Hello world" string in response. Now try accessing the root through your phone and postman. You can directly open it in your phone browser. If it doesn't say "Hello world" then it's confirmed that nothing is wrong with your api but a connection issue b/w your phone and computer.
Upvotes: 0
Reputation: 1395
Try the following solution.
HttpClient
implementation from Default
to Android
Refer to: Xamarin Forms - simple PostAsync() returns error 400 bad request
.
HttpResponseMessage.IsSuccessStatusCode
is satisfied.if (Resultado.IsSuccessStatusCode)
{
//
}
Refer to: https://forums.xamarin.com/discussion/comment/391413/#Comment_391413
.
Upvotes: 0