yazouri_318

i got this exciption message : The remote server returned an error: (401) Unauthorized.
when i try to use the Issue Traking Application the exception occured in

Stream newStream = Req.GetRequestStream();

could you please explain to me the exception and how to handle it and resolve it.



Re: .NET Framework Networking and Communication Issue Traking Application

timvw

It depends on how the server is performing authentication..

(Have a look at the Credentials and ClientCertificates property on the WebRequest instance... In most situations setting Credentials = CredentialCache.DefaultCredentials is already sufficient)





Re: .NET Framework Networking and Communication Issue Traking Application

yazouri_318

the apllication already use :
MyCredentialCache.Add(new Uri(Uri),"NTLM", new System.Net.NetworkCredential(m_username,m_password,m_domain));
isn't that sufficient.





Re: .NET Framework Networking and Communication Issue Traking Application

timvw

As i already wrote: it depends on how the webserver is configured... (Adding a credential to a credentialcache won't help you as long as you don't actually use the credentialcache)

Conclusion: You'll have to provide more information (about the webserver config) and the code you're using.





Re: .NET Framework Networking and Communication Issue Traking Application

yazouri_318

here is the code :

private string Action(string Uri, string Method, string Body, int StartRow,int EndRow)
{
Trace.WriteLine("Entering Action with Method: " + Method + " URI: " + Uri);

string XmlData;

HttpWebRequest Req;

byte[] bytes = null;

try
{
// Create a new credentials cache.
System.Net.CredentialCache MyCredentialCache = new System.Net.CredentialCache();

MyCredentialCache.Add(new Uri(Uri),"NTLM", new System.Net.NetworkCredential(m_username,m_password,m_domain));

// Create request object and assign credentials.
Req = (System.Net.HttpWebRequest) HttpWebRequest.Create(Uri);


// Set the WebRequest credentials value.
Req.Credentials = MyCredentialCache;

if (string.Compare(Method,"SEARCHBYROW",true) == 0)
{
Req.Method = "SEARCH";
Req.AddRange("rows",StartRow,EndRow);
}

else
{
Req.Method = Method;
}

// The body is the destination.
// Rename is actually a move.
if (string.Compare(Method,"RENAME",true) == 0)
{
Req.Headers.Add("Destination",Body);
Req.Headers.Add("Allow-Rename","t");
Req.Method = "MOVE";
Req.Headers.Add("Overwrite","f");
Body = "";
}

else
{
// Property find.
if (string.Compare(Method,"PROPFIND",true) == 0)
{
Req.Headers.Add("depth","0");
}

// Delete method headers.
if (string.Compare(Method,"DELETE",false) == 1)
{
Req.Headers.Add("translate","f");
Req.ContentType = "text/xml";
}

if (Overwrite == true)
{
Req.Headers.Add("Overwrite","t");
}
}

// Get body (payload) and encode it to utf-8.
bytes = Encoding.UTF8.GetBytes((string)Body);

Req.ContentLength = bytes.Length;

Stream newStream = Req.GetRequestStream();

newStream.Write(bytes, 0, bytes.Length);

newStream.Close();
}

catch(Exception ex)
{
throw ex;
}

// Use the Request object to get the response.
try
{
// This line of code will do the request.
WebResponse result = Req.GetResponse();

try
{
ExchangeVersion = result.Headers.Get("MS-WebStorage");
}
catch
{
// It should be there but just in case it's not just set the string to empty.
ExchangeVersion = "";
}

// Get returned XML stream.
Stream ReceiveStream = result.GetResponseStream();

// utf-8 handle it.
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );

// Read the XML to the end.
XmlData = sr.ReadToEnd();

result.Close();
}

catch (Exception ex)
{
// A few of the most important and common error.
//
// 403 - Forbidden - This means there is not enough access to create this folder.
// 405 - Method not allowed - This can mean the user is overwriting a folder (among
// other things).
// 404 - Not found - This is often used to find out if something exists.
// 505 - Server unavailable.
Trace.WriteLine("Exiting Action with an EXCEPTION: " + ex.Message);
throw ex;
}

Trace.WriteLine("Exiting Action Normally.");
return XmlData;
}

the exception occured in this line:
Stream newStream = Req.GetRequestStream();

and i have permission to the http:// which i try to request directly from the IE

thank you in advance for your concern





Re: .NET Framework Networking and Communication Issue Traking Application

yazouri_318

i would like to thank timvw for his try to help me and to post the solution after i found it in support.microsoft .

the solution is:

you have to configer the Directory/Access in IIS of mail server l to enable Integrated Windows Authentication.
which make the web mail ask for your credintial.