WebRequests & 401 Unauthorized errors

G

Guest

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();
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top