Buscar..


Introducción

Aquí veremos Pro-grammatically llamando y utilizando servicios web en ASP.Net C #. Para el propósito, deberá descargar el siguiente ddl, que le proporciona muchas funciones. Descargue ImportJson desde https://drive.google.com/open?id=0B-2bGoHKJvnOckdPUHVjdFZTcFU

Este artículo es muy útil para aquellos de ustedes que van a desarrollar un proyecto utilizando los servicios web de ASP.NET C # / servicios web API. Este artículo también es útil para aquellos que están desarrollando un proyecto usando Xamarin: Desarrollo de aplicaciones móviles

Observaciones

Debes dar referencia de ImportJson dll y restsharp ddl. ImportJson se puede descargar desde aquí https://drive.google.com/open?id=0B-2bGoHKJvnOckdPUHVjdFZTcFU y restsharp.dll obtendrá de internet

Cualquier sugerencia / contacto, por favor tenga en cuenta akhandagale65@gmail.com

Llamando al método GET simple

/// <summary>
    /// Simple Get method
    /// </summary>
    /// <returns> Json formated data </returns>
    public string GetJsonData1()
    {
        IOperations _Obj = ClsOperations.GetOperations();
        string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/"; 
        string jsonResult = _Obj.GetJsonResult(url);
        return jsonResult;
    }

Servicio web de llamada con datos POST / POST Método

        /// <summary>
    /// Post Method with input parameter
    /// </summary>
    /// <returns> Json formated data </returns>
    public string GetJsonData2()
    {
        IOperations _Obj = ClsOperations.GetOperations();
        string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/";
        Dictionary<string, object> objDec = new Dictionary<string, object>();
        objDec.Add("@FirstParameter", "Value1");
        objDec.Add("@SecondParameter", "Value2");
        objDec.Add("@ThirdParameter", "Value3");
        string jsonResult = _Obj.GetJsonResult(url, objDec);
        return jsonResult;
    }

Llamar al servicio web con el método POST / POST de datos (Datos publicados en formato JSON)

/// <summary>
    /// Post Method with Input/ data to post in JSON format 
    /// </summary>
    /// <returns> Json formated data </returns>
    public string GetJsonData3()
    {
        IOperations _Obj = ClsOperations.GetOperations();
        string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/";
        string inputjson = "{\"@FirstParameter\": \"Value1\",\"@SecondParameter\": \"Value2\",\"@ThirdParameter\": \"Value3\"}";
        string jsonResult = _Obj.GetJsonResult(url, null,inputjson );
        return jsonResult;
    }

Llamada de servicio web con salida como objeto IEnumerator

/// <summary>
    /// Post Method with Input/ data to post in JSON format Or you can send dictionary as shown in previous methods
    /// </summary>
    /// <returns> Json formated data </returns>
    public void  GetJsonData4()
    {
        IOperations _Obj = ClsOperations.GetOperations();
        string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/";
        string inputjson = "{\"@FirstParameter\": \"Value1\",\"@SecondParameter\": \"Value2\",\"@ThirdParameter\": \"Value3\"}";
        string jsonResult = _Obj.GetJsonResult(url, null, inputjson);
        IEnumerator objIEnumerator = _Obj.GetJsonEnumerableResult(jsonResult);
        // you can perform further operations on it

    }

Salida del servicio web en formato de lista o formato de tabla de datos

    /// <summary>
        /// Post Method with Input/ data to post in JSON format Or you can send dictionary as shown in previous methods
        /// </summary>
        /// <returns> Json formated data </returns>
        public DataTable  GetJsonData6()
        {
            IOperations _Obj = ClsOperations.GetOperations();
            string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/";
            string inputjson = "{\"@FirstParameter\": \"Value1\",\"@SecondParameter\": \"Value2\",\"@ThirdParameter\": \"Value3\"}";
            IEnumerator objIEnumerator = _Obj.GetJsonEnumerableResult(url, null, inputjson);
            // you can perform further operations on it

            // If you want to convert it in Datatable / List

            List<ClsMyPropertyClass> lst = new List<ClsMyPropertyClass>();
            while (objIEnumerator.MoveNext())
            {
                lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<ClsLineEDoDetails>(objIEnumerator.Current.ToString()));
            } 
// Upto this you will get List , and you can perform operations on it

            // Now if youu want result in datatable, here i written function for List to datatable conversion
            
            return CommonServiceCall.ToDataTable(lst);

        }

Hacer con fuerza el método GET OR POST

/* By Default if you send only url then automatically it will recognize as GET Method and if service having parameters with, Then automatically will convert to POST Method. But I observed some of the services having only URL but are POST Type. For the purpose you can forcefully make the method as you want. As bellow:  */
        /// <summary>
        /// If you want make the service call GET OR POST forcefully then 
        /// </summary>
        /// <returns> Json formated data </returns>
        public void GetJsonData5()
        {
            IOperations _Obj = ClsOperations.GetOperations();
            string url = "http://1.2.3.4:1234/Services/rest/CallService/WebRequest/";
            string inputjson = "{\"@FirstParameter\": \"Value1\",\"@SecondParameter\": \"Value2\",\"@ThirdParameter\": \"Value3\"}";
string  _result =   _ Obj.GetJsonResult(url, null, inputjson, ServiceType.POST);;
                     }


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow