WebRequest.GetResponse() fails with DefaultCredentials in Windows2003

R

Rhywun

The following code snippet (adapted from <a href="http://
authors.aspalliance.com/stevesmith/articles/netscrape.asp">this</a>,
which reads the HTML generated by another page, used to work on
Windows 2000 with whatever version of IIS was current then. Now my
code has been moved to Windows 2003 and (I presume) a newer version of
IIS, and the code no longer works but instead returns a ProtocolError
with a message "The remote server returned an error: (401)
Unauthorized." Inside the Protocol error the message helpfully reads
"Unauthorized".

In web.config, authentication is set to "Windows" and identity
impersonate is on. My authorization simply denies all unauthenticated
users and allows all authenticated users.

Is anyone aware of changes between 2000 and 2003 that break this code?
I've been fiddling with the code and with settings in IIS for a couple
days now without any luck. Many thanks in advance!

PS. The code *does* work from VS2005's built-in web server.

Code:
------------
try
{
String result;
WebRequest request = WebRequest.Create(url);
ICredentials credentials = CredentialCache.DefaultCredentials;
request.Credentials = credentials;

// TODO: Returns 'unauthorized' error on Windows 2003
WebResponse response = request.GetResponse();

using (StreamReader sr = new StreamReader(response.GetResponseStream
()))
{
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
 
R

Rhywun

That code works fine for me in VS 2008

I found another way that works if the page you're scraping is in the
same site (which mine is):

try
{
TextWriter textWriter = new StringWriter();
server.Execute(url, textWriter);
return textWriter.ToString();
}

No messing with credentials.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top