I am developing an app using ASMX webservice. I don't know how to connect it. I have referred here, https://developer.xamarin.com/guides/xamarin-forms/web-services/consuming/asmx. But I am not clear about how to do this.. please can anyone provide simple app using ASMX webservice...
1 Answers
I will share my code as I also needed to connect to my asmx .NET web service and finally managed to do it after a lot of researching and some days trying things with no luck. I saw many posts explaying other ways to do it, but this is the easiest that I've found and the first one that has worked for me.
(I am connecting to a web service that run's in another Visual Studio in debugging mode)
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
httpClient.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/WSLogin");
string wUser = "user";
string wPassword = "password";
string soapstr = string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<WSLogin xmlns=""http://tempuri.org/"">
<wUser>{0}</wUser>
<wPassword>{1}</wPassword>
</WSLogin>
</soap:Body>
</soap:Envelope>", wUser, wPassword);
var response = httpClient.PostAsync("http://localhost:49411/Default.asmx", new StringContent(soapstr, Encoding.UTF8, "text/xml")).Result;
var content = response.Content.ReadAsStringAsync().Result;
You just have to copy this code and modify it to point to your webservice URL, and change the function's name (in my case its WSLogin) and parameters. Or you can just simply copy the entire soap xml that the web service will show if you go to it's asmx file in any browser and then select the function you want to point to.
I read somewhere that iOs needs https to work, but I haven't tried it yet. It works fine on UWP and Android for me.
Hope this helps, this was driving me crazy :)
Edit: you may need this using statements
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Xamarin.Forms;