Search This Blog

Thursday, October 3, 2019

GetResponse from Web Service Sample Code - Testing

Download

Sample(testing)web service url :  
//https://www.w3schools.com/xml/tempconvert.asmx
//http://currencyconverter.kowabunga.net/converter.asmx
//http://www.dneonline.com/calculator.asmx
//http://www.libraryinformationsystem.org/Services/RestService.svc

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Permissions;
using System.ServiceModel;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Xml;
using System.Xml.Serialization;

namespace SPWebServiceConnect
{
    class Program
    {


        [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]

        static void Main()
        {

            var svcurl = ConfigurationManager.AppSettings["SVCServiceUrl"];
            var asmxurl = ConfigurationManager.AppSettings["ASMXServiceUrl"];
            try
            {
                getASMXService(asmxurl);
                Console.WriteLine("........................................");
                Console.WriteLine("..........svcurlstart.............................");
                getSVCService(svcurl);
            }
            catch (Exception ex)
            {
                //  not available at all, for some reason
                Console.Write(string.Format("{0} unavailable: {1}", ex.StackTrace, ex.Message));
            }
            Console.Read();
        }
        /// <summary>
        //https://www.w3schools.com/xml/tempconvert.asmx
        //http://currencyconverter.kowabunga.net/converter.asmx
        //http://www.dneonline.com/calculator.asmx
        //http://www.libraryinformationsystem.org/Services/RestService.svc
        /// </summary>
        /// <param name="url"></param>
        public static void getASMXService(string asmxurl)
        {
            try
            {
                var myRequest = (HttpWebRequest)WebRequest.Create(asmxurl);

                var response = (HttpWebResponse)myRequest.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //  it's at least in some way responsive
                    //  but may be internally broken
                    //  as you could find out if you called one of the methods for real
                    Console.WriteLine(string.Format("{0} Available", asmxurl));

                    Console.WriteLine("Content length is {0}", response.ContentLength);
                    Console.WriteLine("Content type is {0}", response.ContentType);

                    // Get the stream associated with the response.
                    Stream receiveStream = response.GetResponseStream();

                    // Pipes the stream to a higher level stream reader with the required encoding format.
                    StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

                    Console.WriteLine("Response stream received.");
                    Console.WriteLine(readStream.ReadToEnd());
                    response.Close();
                    readStream.Close();
                }
                else
                {
                    //  well, at least it returned...
                    Console.Write(string.Format("{0} Returned, but with status: {1}", asmxurl, response.StatusDescription));
                }
            }
            catch { }
        }
        public static void getSVCService(string svcurl)
        {
            try
            {
                var myRequest = (HttpWebRequest)WebRequest.Create(svcurl);

                var response = (HttpWebResponse)myRequest.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //  it's at least in some way responsive
                    //  but may be internally broken
                    //  as you could find out if you called one of the methods for real
                    Console.WriteLine(string.Format("{0} Available", svcurl));

                    Console.WriteLine("Content length is {0}", response.ContentLength);
                    Console.WriteLine("Content type is {0}", response.ContentType);

                    // Get the stream associated with the response.
                    Stream receiveStream = response.GetResponseStream();

                    // Pipes the stream to a higher level stream reader with the required encoding format.
                    StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

                    Console.WriteLine("Response stream received.");
                    Console.WriteLine(readStream.ReadToEnd());
                    response.Close();
                    readStream.Close();
                }
                else
                {
                    //  well, at least it returned...
                    Console.Write(string.Format("{0} Returned, but with status: {1}", svcurl, response.StatusDescription));
                }
            }
            catch { }
        }
    }
}


No comments:

Post a Comment