Test Client Code to make HttpRequest from RESTFULL Web Services

For POST Method :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace WCFTestClient
{
    class TestClient_POST
    {
        static void Main(string[] args)
        {
            try
            {
            #region Request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(\”Enter URL of Requesting WebService\”);

            string requestXml = \”Enter Request XML\”;
           
            // Requesting Method
            request.Method = \”POST\”;
           
            // Use Content Type based upon your Web Service
            request.ContentType = \”application/x-www-form-urlencoded;charset=UTF-8\”;

            byte[] byteArray = Encoding.UTF8.GetBytes(requestXml);

            request.ContentLength = byteArray.Length;

            Stream dataStream1 = request.GetRequestStream();

            dataStream1.Write(byteArray, 0, byteArray.Length);

            dataStream1.Close();

            #endregion

            #region Response
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine(((HttpWebResponse)response).StatusDescription);

            Stream dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);

            string responseFromServer = reader.ReadToEnd();

            #endregion

            #region Print Resonse
            Console.WriteLine(responseFromServer);
            Console.Read();
            reader.Close();
            dataStream.Close();
            response.Close();
            #endregion
           }
              catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }
    }
}

For GET Method :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace WCFTestClient
{
    class TestClient_GET
    {
         static void Main(string[] args)
        {

            try
            {
                #region Send Request to Service

                HttpWebRequest APIRequest = (HttpWebRequest)WebRequest.Create(\”Enter Requesting Web Service URL\”);

                // ContentType
                APIRequest.ContentType = \”application/xml\”; //or \”text/xml\” or \”text/html\”

                #endregion

                #region Get Response from Service

                // Get Response
                HttpWebResponse APIResponse = (HttpWebResponse)APIRequest.GetResponse();
                StreamReader XMLreader = new StreamReader(APIResponse.GetResponseStream());
                string ResponseXML = XMLreader.ReadToEnd().Trim();

                Console.WriteLine(ResponseXML);
                Console.Read();

                #endregion

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }

        }
    }
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s