The request failed with HTTP status 401: Unauthoriz

rajpatelbcarajpatelbca Member Posts: 178
edited 2010-02-10 in NAV Three Tier
Hi Experts,

i have downloaded an application in visual Studio 2008 to consume paged based web service available in NAV 2009. this application works fine when i am running it on the machine where Database and Microsoft Dynamics NAV Business Service.
but, when i am running this application on other machine reside on local intranet.

example application code is available blow.

Following code is used to pass authentication.
private Assembly BuildAssemblyFromWSDL(Uri webServiceUri)
{
if (String.IsNullOrEmpty(webServiceUri.ToString()))
throw new Exception("Web Service Not Found");

//This following code does not work for NAV Web services so I replaced it
//XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + "?wsdl");
// Start of new code -->
l_http = (HttpWebRequest)WebRequest.Create(webServiceUri.ToString());
_Credntial = new System.Net.NetworkCredential("Username", "password","Domain");

l_http.Timeout = 50000; // Timeout after 5 seconds


l_http.UseDefaultCredentials = false;
l_http.PreAuthenticate = true;
l_http.Credentials = _Credntial;
l_http.PreAuthenticate = true;

//l_http.GetResponse(
//l_http.UseDefaultCredentials = false;
//_Credntial = System.Net.CredentialCache.DefaultCredentials;


//PropertyInfo UseDefaultCred =


HttpWebResponse l_response = (HttpWebResponse)l_http.GetResponse();

Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page

StreamReader l_responseStream = new StreamReader(l_response.GetResponseStream(), enc);

XmlTextReader xmlreader = new XmlTextReader(l_responseStream);
// End of new code <--

ServiceDescriptionImporter descriptionImporter = BuildServiceDescriptionImporter(xmlreader);

return CompileAssembly(descriptionImporter);
}

Following quote shows where error displayed when application run on other machine
public T InvokeMethod<T>( string serviceName, string methodName, params object[] args )
{
// create an instance of the specified service
// and invoke the method
Object obj = this.webServiceAssembly.CreateInstance(serviceName);
Type t1 = l_http.GetType();
Type type = obj.GetType();



// NAV Web services failed here because the service was not using default credentials
// Start of inserted code --->
// Try to tell it to useDefaultCredentials.
PropertyInfo UseDefaultCred = type.GetProperty("UseDefaultCredentials");
//PropertyInfo UseDefaultCred =
UseDefaultCred.SetValue(obj, true,null);

// End of inserted code <---

return (T)type.InvokeMember(methodName,BindingFlags.InvokeMethod,null, obj,args);
}

Error Message :
Error Invoking Method: The request failed with HTTP status 401: Unauthorized.

Kindly suggest me ](*,)

Thanks in Advance,
Experience Makes Man Perfect....
Rajesh Patel

Answers

  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    You can find a lot of information about how to use Dynamic Web Services here:

    http://blogs.msdn.com/freddyk/archive/tags/Dynamic/default.aspx

    If your code works locally and NOT from a different machine, then there shouldn't be anything wrong with your code though.

    Are the machines are not in the same domain, then you cannot use default credentials.
    If they are in the same domain - then the problem might be delegation, spn's etc. - can you start the RTC on that machine?
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • rajpatelbcarajpatelbca Member Posts: 178
    Hi Freddy,

    Thanks fro the quick response.

    your blog is full of webservice examples and it helps me solve the problem.

    =D> =D> =D>


    Thanks once again
    Experience Makes Man Perfect....
    Rajesh Patel
  • Ravi_ThakkarRavi_Thakkar Member Posts: 392
    Hello Rajesh,

    Can you tell the exact reason of this error and how did you solved it????
    Ravi_Thakkar
    Ahmedabad, Gujarat, India
    E Mail : ravi.thakkar@hotmail.com
Sign In or Register to comment.