Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:47:56+00:00 2026-06-04T05:47:56+00:00

I was looking for a sample test application in C# which could test WCF

  • 0

I was looking for a sample test application in C# which could test WCF rest api call?
I couldn’t find one on the internet.
I have some login credentials for that wcf end point as well which I need to pass.
Can someone point me to sample test rest api appln?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:47:58+00:00Added an answer on June 4, 2026 at 5:47 am

    I think using WebClient should work for WCF REST services (if you need, you can use credentials, request headers, etc):

    private void TestService()
    {
        try
        {
            string address = "<WCF REST service address>";
            WebClient client = new WebClient();
            string userName = "<user>";
            string password = "<password>";
            ICredentials creds = new NetworkCredential(userName, password);
            client.Credentials = creds;
            client.Headers[HttpRequestHeader.Authorization] = String.Format("Basic {0}:{1}", userName, password);
            client.Headers[HttpRequestHeader.UserAgent] = @"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)";
            Stream data = client.OpenRead(address);
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            Console.WriteLine(s);
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
            if (ex.Response != null)
            {
                Stream strm = ex.Response.GetResponseStream();
                byte[] bytes = new byte[strm.Length];
                strm.Read(bytes, 0, (int)strm.Length);
                string sResponse = System.Text.Encoding.ASCII.GetString(bytes);
                Console.WriteLine(sResponse);
            }
        }
    }
    

    EDIT: Example for web application which uses Forms authentication.

    My previous example works if the web application allows anonymous users. If not (web.config contains <deny users="?"/> and Anonymous access is enabled in IIS), two requests are required:

    1. the request to the login page to authenticate;

    2. the request to the actual service url.

    The auth cookie should be passed to the second request (we use the CookieContainer object to achieve this).
    The first call passes the user name and password using the POST method. Because my web application uses out-of-the-box login control, I need to pass the view state, event validation, etc. You can get the form data passed during the login from a web browser using Fiddler or Chrome dev tools. This is the code:

    private static void TestService()
    {
        try
        {
            string loginAddress = "<login url>";
            string serviceAddress = "<service url>";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginAddress);
            req.Method = "POST";
            string userName = "<user>";
            string password = "<password>";
            CookieContainer cc = new CookieContainer();
            req.CookieContainer = cc;
    
            StringBuilder sb = new StringBuilder();
            sb.Append(@"__VIEWSTATE=<viewstate>");
            sb.Append(@"&__EVENTVALIDATION=<event validation>");
            sb.Append(@"&ctl00$MainContent$LoginUser$UserName={0}&ctl00$MainContent$LoginUser$Password={1}");
            sb.Append(@"&ctl00$MainContent$LoginUser$LoginButton=Log In");
            string postData = sb.ToString();
            postData = String.Format(postData, userName, password);
            req.ContentType = "application/x-www-form-urlencoded";
            Encoding encoding = new ASCIIEncoding();
            byte[] requestData = encoding.GetBytes(postData);
            req.ContentLength = requestData.Length;
    
            //write the post data to the request
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(requestData, 0, requestData.Length);
                reqStream.Flush();
            }
    
            HttpWebResponse response = (HttpWebResponse)req.GetResponse(); //first call (login / authentication)
    
            req = (HttpWebRequest)WebRequest.Create(serviceAddress);
            req.CookieContainer = cc; //set the cookie container which contains the auth cookie
            response = (HttpWebResponse)req.GetResponse(); //second call, the service request
            Stream data = response.GetResponseStream();
            StreamReader reader = new StreamReader(data);
            string s = reader.ReadToEnd();
            Console.WriteLine(s);
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
            if (ex.Response != null)
            {
                Stream strm = ex.Response.GetResponseStream();
                byte[] bytes = new byte[strm.Length];
                strm.Read(bytes, 0, (int)strm.Length);
                string sResponse = System.Text.Encoding.ASCII.GetString(bytes);
                Console.WriteLine(sResponse);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have googled some time now trying to find a good sample application that
I'm looking to write a sample application speaking to a POP3/SMTP server. Instead of
I am looking for some adequately-complex sample database schemas that I could use for
I am looking for suggestions on database design for a sample jobs listing application.
I have an application which scrapes soccer results from different sources on the web.
We have a java application which essentially performs ETL - reading from and writing
I have a simple isEqualToString test in my application, and for some reason, it
I have VS2010, MVC3 and ASP.NET 4.0 with a simple test mvc application. The
I have been playing with ASP.NET Web API. I am looking to see can
As part of a test bench I'm building, I'm looking for a simple class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.