简体   繁体   中英

asp.net WebAPI call not working from Xamarin.Forms

My Xamarin dosnt call anymore my api after i change the typ of Id from Int to string. But that can't be the reason, before that it works without any problems

When I test the endpoint with Postman, everything works fine

When I debug it, the application runs through to response in the API call and then breaks off without an error message. No exception.

API Call

public async Task<bool> LoginUser(string username, string password)
    {

        _acc.UserName = username;
        _acc.Password = password;

        var httpClient = new HttpClient();
        var json = JsonConvert.SerializeObject(_acc);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        **var response = await httpClient.PostAsync("http://192.168.178.41:45472/api/account/Login", content);**

//here is the break my Web API dosnt call.

        if (!response.IsSuccessStatusCode)
        {
            return false;
        }

        return true;

    }

My DI:

        public async Task LoginUser(string username, string password)
    {

        var result = await _accService.LoginUser(username, password);

        if (result)
        {
            await _navService.NavigateToAsync<Profile_ViewModel>();
        }
        else
        {
            await Application.Current.MainPage.DisplayAlert("nope", "nope", "nope");
        }
    }

My method in the ViewModel

        private async void GoLogin()
    {

        var container = ContainerConfig.CreateContainer();
        using (var scope = container.BeginLifetimeScope())
        {
            var GetService = scope.Resolve<IMyUser_Service>();

            await GetService.LoginUser(UserName, Password);
        }

    }

Xamarin.Droid: The AccessNetworkState permission is required.

Open the AssemblyInfo.cs file under the properties folder and add this:

[assembly: UsesPermission(Android.Manifest.Permission.AccessNetworkState)]

Or update the Android Manifest file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在此处输入图像描述

Xamarin.iOS: App Transport Security enforces secure connections between the app's back-end server and your app. You can add the following code to your app's Info.plist file and It will disable the security defaults that ATS enforces for a given domain:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>192.168.178.41:45472</key> <!-- Your Domain address -->
        <dict>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

Or you can make the following changes to your app's Info.plist file to completely disable ATS for all domains and internet communication:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Hi thanks for the help until now. I make some checks and now i can say its my localhost what is broken. i get only https from conveyor but i need the http version because my app throw me the handshakeexception if i use https.

I try it with azure and now there throw me "System.ObjectDisposedException: 'Cannot access a closed Stream.'" its the same code i only remove the using statement at GoLogin and replace the endpoint to my azure web service.

Still in postman works fine..

I am happy about help

Hi guys i solved my problem. My Model wasnt set right.

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