C# code to get automate response from a url that requires username and password when the URL is requested via web
<p>When I request a page (say: <a href="http://mysite.com/mypage" rel="nofollow">http://mysite.com/mypage</a>) via browser address bar then it prompts me a message [like: Authentication Required: The server <a href="http://mysite.com/mypage:80" rel="nofollow">http://mysite.com/mypage:80</a> requires a username and password...]. When I provide my username and password then it shows the response to the browser. Otherwise it says: User not authorized.</p>

<p>I want to automate this process using C# code so that I can get the response in my code. Since it is prompting username and password so I'm unable to get response via HttpWebRequest/Response.</p>

<p>I've tried with following code block:</p>

<pre><code>       HttpWebRequest webRequest = WebRequest.Create(myUrl) as HttpWebRequest;

       webRequest.Proxy = WebRequest.DefaultWebProxy;

        webRequest.Credentials = new NetworkCredential(userName, password);
        webRequest.Proxy.Credentials = new NetworkCredential(userName, password);

        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        System.IO.Stream stream = response.GetResponseStream();

        System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
        Console.WriteLine(sReader.ReadToEnd());
</code></pre>

<p>Please help me!</p>
