By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

WebRequests & 401 Unauthorized errors

P: n/a
Hi, I'm trying to make a WebRequest to url using the code below.

The url points to a large number XML files on my shared dev server via a virtual folder secured using basic authentication. I did this so I can run the application on my local machine and from the dev server but keep a single copy of the XML files only on dev server. The site uses Basic Authentication and both machines belong to the same domain

However, I ge

"The remote server returned an error: (401) Unauthorized. "

The weird thing this works when I run the code from the local machine all the time, but only ONCE from when I run the app on my dev server. Subsequent requests on the devserver's version return the 401 error. It's really strange in that it seems it loses my credentials on repeated requests. Suggestions on a fix or a better approach? Thanks, Dave

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://mydevserv:1027/Secure/ReportsVirtDir/14330.XML";)
HttpContext cxt = HttpContext.Current
NetworkCredential nc = new NetworkCredential(cxt.Request.ServerVariables["AUTH_USER"], cxt.Request.ServerVariables["AUTH_PASSWORD"],"")
req.Credentials = nc
WebResponse resp = req.GetResponse(); <--ERRO
StreamReader sr = new StreamReader(resp.GetResponseStream())
string strStream = sr.ReadToEnd();
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.