The request failed with HTTP status 401: Unauthoriz

rajpatelbca
Member Posts: 178
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.
Following quote shows where error displayed when application run on other machine
Error Message :
Error Invoking Method: The request failed with HTTP status 401: Unauthorized.
Kindly suggest me ](*,)
Thanks in Advance,
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
Rajesh Patel
0
Answers
-
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.0 -
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 againExperience Makes Man Perfect....
Rajesh Patel0 -
Hello Rajesh,
Can you tell the exact reason of this error and how did you solved it????0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions