401 Unauthorized when accessing basic Authentication site through

  • Thread starter Dominick Baier [DevelopMentor]
  • Start date
D

Dominick Baier [DevelopMentor]

Hi,

Create a NetworkCredentials supplying username and password and set the Credential
property on HttpWebRequest.
 
J

Jazz

Hello,
I have a site(on IIS5, w2k), which is set up to require basic authenticaion.
When I accessed it through http://uid:pwd@myserver/webform1.aspx, it's
working fine. But if I try to access it by the following code, I get "The
remote server returned an error: (401) Unauthorized."

Any idea.

Thanks.
Jazz

using System;
using System.Net;
using System.Xml ;
using System.IO;

namespace TestHTTPA
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string abc=SendRequest(@"<test>AAA</test>");
}

static private string SendRequest(string stringToBeSent)
{
StreamWriter writer=null;
StreamReader r=null;
Stream stm=null;

uri=@"http://uid:pwd@myserver/webform1.aspx";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

request.Method = "POST";
//request.ContentType = "text/xml";
request.ContentType = "text/html";

// send the request
writer = new StreamWriter(request.GetRequestStream());
writer.Write(stringToBeSent);
writer.Close();

//get the httpresponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response != null)
{
stm = response.GetResponseStream();
r = new StreamReader(stm);
// process return doc here. For now, we'll just send the XML out to the
browser ...
return r.ReadToEnd();

}
else
{
return "Error!";
}
}
catch(WebException we)
{
string a= we.Message.ToString();
return we.Message.ToString();
}
catch(Exception e)
{
return e.ToString();
}
finally
{
r.Close();
stm.Close();
writer.Close();
}
}


}
}
 
J

Jazz

Hello Dominick,
It works !
Thanks,
Jazz

Dominick Baier said:
Hi,

Create a NetworkCredentials supplying username and password and set the Credential
property on HttpWebRequest.

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com
Hello,
I have a site(on IIS5, w2k), which is set up to require basic
authenticaion.
When I accessed it through http://uid:pwd@myserver/webform1.aspx, it's
working fine. But if I try to access it by the following code, I get
"The
remote server returned an error: (401) Unauthorized."
Any idea.

Thanks.
Jazz
using System;
using System.Net;
using System.Xml ;
using System.IO;
namespace TestHTTPA
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string abc=SendRequest(@"<test>AAA</test>");
}
static private string SendRequest(string stringToBeSent)
{
StreamWriter writer=null;
StreamReader r=null;
Stream stm=null;
uri=@"http://uid:pwd@myserver/webform1.aspx";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
//request.ContentType = "text/xml";
request.ContentType = "text/html";
// send the request
writer = new StreamWriter(request.GetRequestStream());
writer.Write(stringToBeSent);
writer.Close();
//get the httpresponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response != null)
{
stm = response.GetResponseStream();
r = new StreamReader(stm);
// process return doc here. For now, we'll just send the XML out
to the
browser ...
return r.ReadToEnd();
}
else
{
return "Error!";
}
}
catch(WebException we)
{
string a= we.Message.ToString();
return we.Message.ToString();
}
catch(Exception e)
{
return e.ToString();
}
finally
{
r.Close();
stm.Close();
writer.Close();
}
}
}
}
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top