How to communicate with WEB API in Windows Phone? - windows-phone-7

I am working with push notifications in windows phone. Notification channel uri is changing while re installing the app in the windows mobile. so there is a problem to send notification to the channel url.
By using web service i am storing the url in database.
I want to update the uri if it is changed using device id.
To do that, i have a WEB API method [HTTPGET] to check the deviceid availability in the database. It is developed to return True/False.
Now my question is, How can we communicate with the service and how can we handle the web api bool return type using C# in windows phone?
Thank You.

To communicate with the service with httpget request you have to use HttpClient class
Here how I do:
HttpClient client = new HttpClient();
string result=await client.GetStringAsync(url);
note that the event which call the web service must be declared async, because the httpclient is purely Asynchronous

Related

NestJS Rest API with Websockets

I want to create a Rest API server with NestJS for management system for a restaurant Kiosk Android. There will be a CRUD for order and also and endpoint to change settings to the Kiosk.
The Kiosk (Android) will be connected to the NestJS through web socket. How can I pass the data from PATCH /settings endpoint where I receive from rest API and pass it thourgh the websocket to the android app? Is this concept valid?
I created the setting endpoint but not sure how can I pass it to the websocket
Use case:
The admininistrator from a React App makes a request to the NestJs Rest API (PATCH /settings with body { active: false })
The NestJS API sends through web socket to the Android App an event to make the kiosk to turn off with active to false
Is this concept valid?

Using Windows Authentication to call a Web API from a Windows Store application

Here is the context of my issue: I am developing a Windows Store application that will be side-loaded on several tablets our client is planning to use. This tablet app will make calls into a Web API which in turn will do CRUD operations with a repository (SQL Server via EntityFramework). Everything has to use Windows Authentication. The tablets are Dell running Windows 10. Each user will log in with its own active domain credentials. The tablet will pass the credentials to the Web API which in turn will pass them to the repository (database). Based on the credentials, the repository will determine the group the user belongs to and based on that, it will give access to resources the user is allowed to access.
My Web API works fine. I tested it with Fiddler. For the HTTP GET request, I want to test, I checked the "Automatically Authenticate" checkbox and I can see the three successive messages, first two returning with 401 and the third returning HTTP Code 200, along with the data I requested.
However, when I try to call it from my Windows Store app, I only send one GET Request, the first one. My Web API is hosted in IIS Express. At the advice of a very distinct member of this group, I configured IIS Express to expose the Web API using the IP address of my development machine rather than "localhost". Before doing this I wouldn't see any GET Requests hitting the server, not even the first one.
I know that Windows Authentication uses the NTLM scheme and it requires a negotiation process, hence the 3 messages Fiddler sends initially to the server?
How do I need to write my Web API Client code to bypass this negotiation? I spent all morning and afternoon reading solutions to my problem, here on SO and many other websites, but somehow, I still did not get it to work. I must be missing something and I don't know what. Here is my code on the Web API Client side:
var authHandler = new HttpClientHandler()
{
Credentials = CredentialCache.DefaultNetworkCredentials,
ClientCertificateOptions = ClientCertificateOption.Automatic
};
var webApiClient = new HttpClient(authHandler)
{
BaseAddress = new Uri(_serviceUri), // _serviceUri is a string that looks like this "http://10.120.5.201:53045"
};
webApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await webApiClient.GetAsync("api/user");
My call never returns from GetAsync, but in Fiddler, I can see only the first GET Requests being posted to the server. How can I get my client to follow up, behind the scenes, just like Fiddler does, and send the other two messages so that, eventually, the third one would return with a response HTTP 200 code and with the data I receive in Fiddler?
If anyone can point me in the right direction, I would be highly appreciative.
The problem was that the "Enterprise Authentication" setting was not set in the Capabilities tab of the Package.appxmanifest file of my app. It took me a while to figure out that that was the problem, but as soon as I checked it, my app started using Windows Authentication.

Azure AD permissions and the PUT verb

We have an ASP.Net MVC web site that talks to a WebAPI service. This is authenticated at the application level by using application authentication in Azure AD.
This all works fine for GET and POST operations, but the same tokens don't work for PUT operations, is there any good reason for this?
EDIT - example of how we are calling the service.
We're using an HttpClient to make a call to the WebAPI
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return await _httpClient.PutAsync(url, content);
Then we get back a HttpResponseMessage which has status code of Unauthorized.
We're not calling the Graph API directly but using the AAD instance to authenticate the calling of one service by another.

Confuse in using which version of HttpClient in Windows.Web.Http and System.Net.Http;

I'm new to Web API and Windows Store App 8.1. I'm developing a Windows Store app that communicates to Web API. When I try to write the following code:
// server:53452/api/demo?ReportingMonth=10&ReportingYear=2013"
using (HttpClient httpClient = new HttpClient())
{
using (HttpResponseMessage response = await httpClient.GetAsync(new Uri("address")))
{
string result = await response.Content.ReadAsStringAsync();
var prodt = JsonConvert.DeserializeObject<ObservableCollection<Statuses>>(result);
return prodt;
}
}
I see that HttpClient is in both Windows.Web.Http and System.Net.Http. Which namespace should I use?
If I pick the System.Net.Http namespace, when I try to call my Web API, which is Windows Authenticate enabled, the cursor will not return back to the client, remaining in unknown state. Not sure about why I'm not receiving the response.
address = "abc.com:53452/api/demo?ReportingMonth=10&ReportingYear=2013"
using (HttpResponseMessage response = await httpClient.GetAsync(new Uri(address)))
If i use HttpClient from the Windows.Web.Http, windows store app asks me to enter credentials, and even though I entered my credentials correctly, system keeps prompting to enter the credentials. Can anyone explain why that happens?
Demystifying HttpClient APIs in the Universal Windows Platform
Which one should I use?
Since both of these APIs are available in UWP, the biggest question
for HTTP developers is which one to use in their app. The answer is
that it depends on a couple of factors:
Do you need to integrate with native UI for collecting user credentials, control HTTP cache read and write behavior; or pass in a
specific SSL client certificate for authentication?
If yes – then use Windows.Web.Http.HttpClient. At the time of this
writing, the Windows.Web.Http API provides greater control over HTTP
settings in UWP than the System.Net.Http API. In future versions, the
System.Net.Http API may also be enhanced to support these features on
UWP.
Do you intend to write cross-platform .NET code (across UWP/ASP.NET 5/iOS and Android)?
If yes – then use System.Net.Http API. This allows you to write code
that you can re-use on other .NET platforms such as ASP.NET 5 and .NET
Framework desktop applications. Thanks to Xamarin, this API is also
supported on iOS and Android, so you can reuse your code on these
platforms as well.
To perform HTTP authentication, instead of HttpClientHandler use HttpBaseProtocolFilter:
var filter = new HttpBaseProtocolFilter();
filter.ServerCredential = new
Windows.Security.Credentials.PasswordCredential(uri.ToString(), "foo", "bar");
var client = new HttpClient(filter);

Call Windows Service from Controller

I have a windows service created and installed. I want to call it from my MVC controller, as I am implementing MSMQ messaging service, so need to call Windows service.
Ideally the windows service should host a WCF service that your web application can contact. From there, it's a matter of creating a client, making the WCF call (to the service) and sending/receiving the information necessary.
Sorry, didn't see MSMQ until later. How about something like:
MessageQueueTransaction msgTx = new MessageQueueTransaction();
MessageQueue msgQ = new MessageQueue(#".\private$\Orders");
msgTx.Begin();
try
{
msgQ.Send("First Message",msgTx);
msgQ.Send("Second Message",msgTx);
msgTx.Commit();
}
catch
{
msgTx.Abort();
}
finally
{
msgQ.Close();
}
Then, of course, have a method of reading the messages in the service.
there is a .net library to do that as push notifications, it's name is signalr

Resources