OK, here is my latest sob story...
How I learned to hate and despise Microsoft's naming conventions!
(I was trying for a "How I learned to stop worrying and love the bomb" type title -- and failed)
I've gotten big into Unit Testing lately and have been working with NUnit to do so (not directly a Microsoft product), and I decided to create some unit tests for a web app I've been writing.
Test 1: see if the web server is up! Should be a simple test, just grab a file through the web server. Note: I've actually had a case or two where the server was running fine, but IIS had blown a hose loose -- couldn't ping it, couldn't restart the service, had to reboot the computer.
OK, I'm sure there is a class to do just that, no problem. I JFGI (new acronym used by one of my colleges -- Just F**king Google It) for the page. I'll try a search term like ".NET C# web winforms download".
No luck.
I try again with different parameters. No luck.
Basically, all that I keep getting are references to ASP.NET development (actually creating a webform -- not what I'm trying to do here) and .NET domain names (like .org and .com)! Basically, any page that had anything to do with web development, c#, and happened to list a web page with a .net domain root -- it got picked up! Try it, just do a search on .NET, or .NET framework.
Next try System.Web -- I thought that would be a likely location for such a class. May the force be with you on that one.
Oh ya, there were also several thousand references to the Cassini web server! -- written in C# for displaying ASP.NET pages. Kind of cool...but NOT what I as looking for!
I search Microsoft.com. No luck there either. In some ways this was even worse.
Half a day later, I finally found what I was looking for: HttpWebRequest in the System.Net namespace. Gonna have to look into that namespace some more later. But right now, I gotta get some work done. I'm a little bit behind now.
In fact, here is the code, should anyone care. You need to include the following namespaces: using System.IO;
using System.Net;
using System.Text;
private string RetrieveWebPage(string url)
{
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url);
oRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
Stream oStream = oResponse.GetResponseStream();
// read the stream in the encoded "utf-8" format
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader oReader = new StreamReader(oStream);
string sPage = "";
while (oReader.Peek() > -1)
sPage += oReader.ReadLine() + "\n";
return sPage;
}
No comments:
Post a Comment