HttpWebRequest & (401) Unauthorized http status code

G

Guest

I would appreciate any help with the following, havn't found any answers for this in my research to date.

I am trying to retreive a web page (a htm file intially) from a vdir on an intranet site using the following code, where the vdir has "Integrated Windows authentication" turned on but "Anonymous access" turned off, i get the following message
The remote server returned an error: (401) Unauthorized.
If i use the browser i can see the page with no problems.
If i enable "Anonymous access", the below will work but this completly defeats the purpose of turning on "Integrated Windows authentication".
I have seen web posts where people using some COM components in VB6.0 can get this to work but for some reason the HttpWebRequest being used below will not work event though i have set the PreAuthenticate property.


private static void HttpGet(string strUrl)
{
HttpWebRequest _Request = null;
HttpWebResponse _Response = null;
Stream _ResponseStream = null;
StreamReader _ResponseStreamReader = null;
string _strResponse = string.Empty;


try
{
// Instantiate request setting header properties
_Request = (HttpWebRequest) WebRequest.Create(strUrl);
_Request.Method = "GET";
_Request.ContentType = "text/xml";

// Proxy
_Request.PreAuthenticate = true;
_Request.Proxy = WebProxy.GetDefaultProxy();
_Request.Proxy.Credentials = CredentialCache.DefaultCredentials;

// Get response
_Response = (HttpWebResponse) _Request.GetResponse();
_ResponseStream = _Response.GetResponseStream();
_ResponseStreamReader = new StreamReader(_ResponseStream, Encoding.UTF8);
_strResponse = _ResponseStreamReader.ReadToEnd();

// Close response stream
_ResponseStream.Close();

// Write out response
Console.WriteLine(_strResponse);
}
catch (Exception GenericException)
{
Console.WriteLine(GenericException.Message);
}
}




Thanks in advance
Pat
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top