Re: 401 Unauthorized when accessing basic Authentication site thro



Hello Dominick,
It works !
Thanks,
Jazz

"Dominick Baier [DevelopMentor]" wrote:

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



.



Relevant Pages

  • Re: FileUpload
    ... Mich interessier eigentlich nur ob man das was im Beispielcode mit der ... HttpWebRequest Klasse gemacht wird auch mit WebClient.UploadFile möglich ist. ... private bool UploadFileWithPost(string filename, string issueID) ... WebClient webClient = new WebClient; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: FileUpload
    ... Mich interessier eigentlich nur ob man das was im Beispielcode mit der ... HttpWebRequest Klasse gemacht wird auch mit WebClient.UploadFile möglich ... private bool UploadFileWithPost(string filename, string issueID) ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: 401 Unauthorized when accessing basic Authentication site through
    ... Create a NetworkCredentials supplying username and password and set the Credential property on HttpWebRequest. ... static private string SendRequest ... StreamWriter writer=null; ... HttpWebResponse response = request.GetResponse; ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: HttpRequest Question
    ... The code that Patrice provided seems to be doing the same thing. ... What I believe to be happening is that the HttpWebRequest Object is parsing the XML much like a browser would. ... If you browse out to the link that the code is requesting and "view source" you'll see the raw xml, however if you look at what my code is outputting you can see that you get the transformed document instead. ... //Convert the data into a string ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Modify exchange security descriptor
    ... You could use the security module in the SDK. ... > HttpWebRequest request = WebRequest.Create; ... > HttpWebResponse response = request.GetResponse; ... > string strStatus = response.StatusCode.ToString; ...
    (microsoft.public.exchange2000.development)

Quantcast