informed
Posts: 128
Joined: Mon Oct 26, 2015 6:24 pm

Trouble Calling WebAPI from Client

Mon Nov 09, 2015 3:52 pm

Hi,

I am trying to upload data to my server by calling a web api from my IoT device.

This is my client:

Code: Select all

              using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(Shared.URL);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
                    var response = client.PostAsync(route + "?" +
                        GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef + "&" +
                        GeneralTags.CONTACT_NO + "=" + customer.ContactNo + "&" +
                        GeneralTags.CUSTOMER_REF + "=" + customerLookUpResult.CustomerRef + "&" +
                        GeneralTags.DOE + "=" + customer.DOE + "&" +
                        GeneralTags.EMAIL + "=" + customer.Email + "&" +
                        GeneralTags.FNAME + "=" + customer.FName + "&" +
                        GeneralTags.SNAME + "=" + customer.SName + "&" +
                        GeneralTags.ADDRESS_REF + "=" + addressLookUpResult.AddressRef +
                        GeneralTags.ADD1 + "=" + customer.Add1 + "&" +
                        GeneralTags.ADD2 + "=" + customer.Add2 + "&" +
                        GeneralTags.ADD3 + "=" + customer.Add3 + "&" +
                        GeneralTags.TOWN + "=" + customer.Town + "&" +
                        GeneralTags.COUNTY + "=" + customer.County + "&" +
                        GeneralTags.PCODE + "=" + customer.PCode + "&" +
                        GeneralTags.COUNTRY + "=" + customer.Country
                       , null).Result;

                    response.EnsureSuccessStatusCode();
                    string json = await response.Content.ReadAsStringAsync();
                    var objs = JArray.Parse(json);
                    return JsonConvert.DeserializeObject<Model.CustomerAddress>(response.Content.ReadAsStringAsync().Result);
                }
This is my api controller:

Code: Select all

        public Models.CustomerAddress Add(Guid CompanyRef, string ContactNo, Guid CustomerRef, string DOE, string Email, string FName, string SName,
            Guid? addressRef, string add1, string add2, string add3, string town, string county, string pCode, string country)
        {
            var res= customerRepository.Add(CompanyRef, ContactNo, CustomerRef, DOE, Email, FName, SName,
                 addressRef,  add1,  add2,  add3,  town,  county,  pCode,  country);
            return new Models.CustomerAddress {
                AddressRef =res.AddressRef,
                CustomerRef =res.CustomerRef,
                CustomerExists=  (res.CustomerRef==CustomerRef)? true : false
            };
        }
I get the error 405 - not allowed?

Thanks Anyone :)

UndergroundFun
Posts: 44
Joined: Wed Jun 17, 2015 12:57 pm

Re: Trouble Calling WebAPI from Client

Thu Nov 12, 2015 2:15 pm

Do you have anonymous authentication enabled on your target service?

Return to “Windows 10 for IoT”