C# C# Streams and Data Processing Streaming Data on the Net Become Zen

Daniel Hildreth
Daniel Hildreth
16,170 Points

Become Zen C# Streams Challenge 2

I am stumped at the last part of this question. I am not sure what I'm doing wrong. Can someone help and explain this to me?

The question asks:

In the BecomeZen method, create a new WebClient object named webClient. Remember that WebClient is disposable, so wrap it in a using statement. We'll need to add a user-agent header, which specifies the "agent" that is sending the request. In most cases, it's a browser, but in our case, it's this code challenge! Inside the using block, add a Header with a name of "User-Agent" and a value of "CodeChallenge".

Program.cs
using System;
using System.IO;
using System.Net;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(BecomeZen());
        }
        public static string BecomeZen()
        {
            string zenResponse = "";

            using(var webClient = new WebClient())
            {
            webClient.Headers.Add("User-Agent", "CodeChallenge");
            string response = webClient.DownloadString(string.Format("https://api.github.com/zen", zenResponse));
            }

            return zenResponse;
        }
    }
}
Daniel Hildreth
Daniel Hildreth
16,170 Points

I passed this challenge by passing this code into it. It is very similar to what I previously had. Can someone please explain the differences between my two codes? The failed way kept wanting me to pass in that GitHub address to the DownloadString method.

//Correct Way

using System;
using System.IO;
using System.Net;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(BecomeZen());
        }
        public static string BecomeZen()
        {
            string zenResponse = "";

            using(var webClient = new WebClient())
            {
            webClient.Headers.Add("User-Agent", "CodeChallenge");
            string response = webClient.DownloadString("https://api.github.com/zen");
            }

            return zenResponse;
        }
    }
}
// Failed Way

using System;
using System.IO;
using System.Net;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(BecomeZen());
        }
        public static string BecomeZen()
        {
            string zenResponse = "";

            using(var webClient = new WebClient())
            {
            webClient.Headers.Add("User-Agent", "CodeChallenge");
            string response = webClient.DownloadString(string.Format("https://api.github.com/zen", zenResponse));
            }

            return zenResponse;
        }
    }
}

5 Answers

Steven Parker
Steven Parker
201,315 Points

:point_right: Your first try has an invalid call to string.Format:

string.Format("https://api.github.com/zen", zenResponse)

While this may not generate a compiler error, you can see that there is a second argument to be included in the format but the format string has no placeholder ("{0}") to put it into. At this point, zenResponse is just an empty string so it would not make sense to include it anyway.

As you realized, only the plain string itself is needed as the argument to DownloadString.

:warning: However — there's an even more serious error here!

The challenge failed to notice that you did not pass back the collected information. Look at this line:

            string response = webClient.DownloadString("https://api.github.com/zen");

You are creating a new variable "response" here which is then immediately disposed as you exit the using. The function would always return an empty string! But what the challenge specifies would look like this instead:

            zenResponse = webClient.DownloadString("https://api.github.com/zen");

Then, zenRespose is returned by the function.

:beetle: You might want to report this as a bug on the Support page. It may get you an Exterminator badge!

Steven's method is not working too. I think there might be something wrong coz I struggled and ended up using Steven's method with no luck

`` using System; using System.IO; using System.Net;

namespace Treehouse.CodeChallenges { class Program { static void Main(string[] args) { Console.WriteLine(BecomeZen()); } public static string BecomeZen() { string zenResponse = "";

        using(var webClient = new WebClient())
        {
        webClient.Headers.Add("User-Agent", "CodeChallenge");
        zenResponse = webClient.DownloadString("https://api.github.com/zen");
        }

        return zenResponse;
    }
}

} ``

using System; using System.IO; using System.Net;

namespace Treehouse.CodeChallenges { class Program { static void Main(string[] args) { Console.WriteLine(BecomeZen()); } public static string BecomeZen() { string zenResponse = "";

        using(var webClient = new WebClient())
        {
        webClient.Headers.Add("User-Agent", "CodeChallenge");
        string response = webClient.DownloadString("https://api.github.com/zen");
        }

        return zenResponse;
    }
}

}

James Churchill
STAFF
James Churchill
Treehouse Teacher

Daniel,

Thanks so much for reporting this issue to our support. And thanks to Steven for his excellent review and feedback!

I'm happy to report that this issue has been resolved in this code challenge.

Thanks ~James