Plugin.XamarinWebApi 1.0.0

dotnet add package Plugin.XamarinWebApi --version 1.0.0
NuGet\Install-Package Plugin.XamarinWebApi -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.XamarinWebApi" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plugin.XamarinWebApi --version 1.0.0
#r "nuget: Plugin.XamarinWebApi, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plugin.XamarinWebApi as a Cake Addin
#addin nuget:?package=Plugin.XamarinWebApi&version=1.0.0

// Install Plugin.XamarinWebApi as a Cake Tool
#tool nuget:?package=Plugin.XamarinWebApi&version=1.0.0

XamarinWebApi

Plugin for Xamarin Accessing WebApi Web Api (using JSON)

This plugin is to ease developer to access web api in xamarin. You can send request, receive response and extract cookies from web api.

Note: All of your projects under a solution must be using Xamarin Forms 3.3.0.912540 or later All projects must be using the same version of Xamarin Forms if not, there will be token error

After installing the plugin into your project from nuget: https://www.nuget.org/packages/Plugin.XamarinWebApi/1.0.0#

  1. Study the web api request in JSON format. For example:
{
  "UserName": "karim@mail.com",
  "UserAgentCode": null,
  "ManufacturerCode": null,
  "AuthenticationToken": null,
  "DeviceId": null,
  "RequestTrxId": "",
  "RequestRetryNo": "",
  "AppVersion": null,
  "RequestObject": {
    "Username": "karim@mail.com",
    "Password": "Abc@12345"
  }
}
  1. Create two classes that reflect the JSON structure for web api request. For example:
 public class WS_RequestHeaderLogin
    {
        public string UserName;
        public string UserAgentCode;
        public string ManufacturerCode;
        public string AuthenticationToken;
        public string DeviceId;
        public string RequestTrxId;
        public string RequestRetryNo;
        public string AppVersion;
        public WS_LoginUser RequestObject;
    }
    
public class WS_LoginUser
    {
        public string Username;
        public string Password;
    }
    
  1. If you want to format the response from JSON and extract into any class, study the JSON format of the response first, for example:
 {
   "RequestUserName": "karim@mail.com",
   "RequestDeviceId": null,
   "RequestTrxId": 0,
   "RequestRetryNo": 0,
   "RequestAppVersion": null,
   "ResponseStatus": "S",
   "ResponseErrorNo": null,
   "ResponseErrorDetail": null,
   "AuthenticationToken": null,
   "ResponseTrxId": 0,
   "ResponseMessage": "User karim@mail.com successfully login.",
   "ResponseValue": 0
}
  1. Create a class that reflects JSON structure of the response, for example:
public class WS_ResponseHeader
    {
        public object ResponseObject;
        public object RequestUserName;
        public object RequestDeviceId;
        public object RequestTrxId;
        public object RequestRetryNo;
        public object RequestAppVersion;
        public object ResponseStatus;
        public object ResponseErrorNo;
        public object ResponseErrorDetail;
        public object AuthenticationToken;
        public object ResponseTrxId;
        public object ResponseMessage;
        public object ResponseValue;
    }
    
  1. Once done, insert function below to any page that needs to access the web api:
 public async void accessWebApi()
        {
            WS_LoginUser wsLoginDetails = new WS_LoginUser();
            wsLoginDetails.Username = "karim@mail.com";
            wsLoginDetails.Password = "Abc@12345";

            WS_RequestHeaderLogin wsReqHeader = new WS_RequestHeaderLogin();
            wsReqHeader.UserName = wsLoginDetails.Username;
            wsReqHeader.RequestObject = wsLoginDetails;

            try
            {
                //string responseStr = await Task.Factory.StartNew(XamWebApi.AccessWebApiAsync, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

                //access and getting response from webApi
                //XamWebApi.AccessWebApiAsync(<base url>, <timeout in seconds>, <web api url>, <request header class>)
                string responseStr = await XamWebApi.AccessWebApiAsync("http://localhost/", 30, "http://localhost/edfapi/user/login", wsReqHeader);

                //convert response from JSON to class WS_ResponseHeader, these lines is optional
                WS_ResponseHeader responseObject = new WS_ResponseHeader();
                if (responseStr != "")
                {
                    object jsonTest = JsonConvert.DeserializeObject(responseStr);
                    responseObject = JsonConvert.DeserializeObject<WS_ResponseHeader>(responseStr);                    
                }

                //retrieve all cookies from webApi
                foreach (Cookie cookie in XamWebApi.cookieCollection)
                {
                    string cookieName = cookie.Name;
                    string cookieValue = cookie.Value;

                    if (cookieName == ".ASPXFORMSAUTH") //search cookie by name
                    {

                    }
                }

                var check = "";
            }
            catch(Exception ex)
            {
                var exMsg = ex.InnerException.Message;
            }
            
        }
        
  1. Please have a look at the sample code here: https://github.com/magiciangambit/XamarinWebApi/
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed.  monoandroid81 is compatible. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap10.0.16299 is compatible. 
Xamarin.iOS xamarinios was computed.  xamarinios10 is compatible. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 668 11/15/2018

version 1.0.0.0