consuming webservice with java

hans51hans51 Member Posts: 5
edited 2013-03-15 in NAV Three Tier
Hello,

i try to consume a nav2009 webservice with java.
My problem is the authentification on the Webserver, when I call the service then i get the message
"HTTP/1.1 401 Unauthorized"

When i open the WSDL over Firefox or IE6 i get a blank Page.
I've try to debug it with FireBug and i have the same Error (401 Unauthorized).

It works only correct with IE7 and C#.

I found nothing in the MSDN or NAVdocumentation aboute the Authentification on the Webserver.

Does any body know how the Authentification works?
Use Microsoft NTLMv2 for Authentification?
Is it possible to config the Webserver Authentification?

regards
Hans51

Comments

  • kinekine Member Posts: 12,562
    The WebServices are usig WIndows Authentication to authenticate the user. It means that the account used for connectig must have permissions in NAV. In VS in C# the webservice have property "UseDefaultCredentials" or "Credentials" to set the credentials which are used when connecting. May be something like this will be in the Java... :wink:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    On my blog you will find a couple of samples on how to access web services from Javascript - it isn't exactly what you are looking for - but it shows how to access WS from something different than C# (where the add web reference tends to help you a lot).
    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.
  • hans51hans51 Member Posts: 5
    .
    kine wrote:
    The WebServices are usig WIndows Authentication to authenticate the user. It means that the account used for connectig must have permissions in NAV. In VS in C# the webservice have property "UseDefaultCredentials" or "Credentials" to set the credentials which are used when connecting. May be something like this will be in the Java... :wink:

    Yes i have Credentials in Java and i have try any auth.-methods to connect to the webserver.
    I get every time the error message:

    HTTP/1.1 401 Unauthorized
    Content-Length: 0
    Server: Microsoft-HTTPAPI/1.0
    WWW-Authenticate: Negotiate

    freddy.dk wrote:
    On my blog you will find a couple of samples on how to access web services from Javascript - it isn't exactly what you are looking for - but it shows how to access WS from something different than C# (where the add web reference tends to help you a lot).

    The Problem is not the Webservice, i can create the Stubs proper.
    The best way is to diable the Authentification, my Server is in a DMZ.
    Is it possible to use a IIS/Tomcat to publish the Webservices, or disable the Auth. by the configfile of NAV Webserver?
  • NickANickA Member Posts: 32
    I'm in a similar situation (I have a client trying to use Java to consume NAV 2009 web services, I know next to nothing about Java personally) and am currently working through it with Microsoft support. Their advice so far is to provide an example here...

    http://java.sun.com/javase/6/docs/techn ... -auth.html

    And utter the cryptic phrase "Where our implementation is using Http SPNEGO Negotiate".

    Best of luck, please post an update here if you get any further! :D
  • ara3nara3n Member Posts: 9,256
    hans51 wrote:
    Hello,

    When i open the WSDL over Firefox or IE6 i get a blank Page.
    I've try to debug it with FireBug and i have the same Error (401 Unauthorized).

    It works only correct with IE7 and C#.

    Hans51

    Hello. In order to see the webservice in Firefox, you need to follow the following direction

    Out of the box the Firefox negotiate authentication is disabeled, to make it work you have to configure your Firefox Browser to use the negotiate authentication first.

    1. Start firefox, enter about:config to the address field.
    2. choose the network.negotiate-auth.trusted-uris parameter, set this value to the domain or machine name you want to activate Negotiate for.


    http://grolmsnet.de/kerbtut/firefox.html
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • hans51hans51 Member Posts: 5
    NickA wrote:
    I'm in a similar situation (I have a client trying to use Java to consume NAV 2009 web services, I know next to nothing about Java personally) and am currently working through it with Microsoft support. Their advice so far is to provide an example here...

    http://java.sun.com/javase/6/docs/techn ... -auth.html

    And utter the cryptic phrase "Where our implementation is using Http SPNEGO Negotiate".

    Best of luck, please post an update here if you get any further! :D

    I have try the Guide. Here is the Debug output:
    2009/01/28 11:31:47:374 CET [DEBUG] HttpClient - Java version: 1.6.0_04
    2009/01/28 11:31:47:374 CET [DEBUG] HttpClient - Java vendor: Sun Microsystems Inc.
    2009/01/28 11:31:47:390 CET [DEBUG] HttpClient - Operating system name: Windows XP
    2009/01/28 11:31:47:390 CET [DEBUG] HttpClient - Operating system architecture: x86
    2009/01/28 11:31:47:390 CET [DEBUG] HttpClient - Operating system version: 5.1
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SUN 1.6: SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS keystore; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunRsaSign 1.5: Sun RSA signature provider
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunJSSE 1.6: Sun JSSE provider(PKCS12, SunX509 key/trust factories, SSLv3, TLSv1)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunJCE 1.6: SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunJGSS 1.0: Sun (Kerberos v5, SPNEGO)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunSASL 1.5: Sun SASL provider(implements client mechanisms for: DIGEST-MD5, GSSAPI, EXTERNAL, PLAIN, CRAM-MD5; server mechanisms for: DIGEST-MD5, GSSAPI, CRAM-MD5)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - XMLDSig 1.0: XMLDSig (DOM XMLSignatureFactory; DOM KeyInfoFactory)
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunPCSC 1.6: Sun PC/SC provider
    2009/01/28 11:31:47:468 CET [DEBUG] HttpClient - SunMSCAPI 1.6: Sun's Microsoft Crypto API provider
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.useragent = Jakarta Commons-HttpClient/3.1
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.protocol.version = HTTP/1.1
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.connection-manager.class = class org.apache.commons.httpclient.SimpleHttpConnectionManager
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.protocol.cookie-policy = default
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.protocol.element-charset = US-ASCII
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.protocol.content-charset = ISO-8859-1
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.method.retry-handler = org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@787171
    2009/01/28 11:31:47:468 CET [DEBUG] DefaultHttpParams - Set parameter http.dateparser.patterns = [EEE, dd MMM yyyy HH:mm:ss zzz, EEEE, dd-MMM-yy HH:mm:ss zzz, EEE MMM d HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE, dd-MMM-yyyy HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy HH:mm:ss z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy HH-mm-ss z, EEE dd-MMM-yy HH:mm:ss z, EEE dd MMM yy HH:mm:ss z, EEE,dd-MMM-yy HH:mm:ss z, EEE,dd-MMM-yyyy HH:mm:ss z, EEE, dd-MM-yyyy HH:mm:ss z]
    2009/01/28 11:31:47:484 CET [DEBUG] DefaultHttpParams - Set parameter NTLM = [NTLM]
    2009/01/28 11:31:47:484 CET [DEBUG] DefaultHttpParams - Set parameter http.authentication.preemptive = false
    2009/01/28 11:31:47:531 CET [DEBUG] HttpConnection - Open connection to BDI12817:7047
    2009/01/28 11:31:47:546 CET [DEBUG] header - >> "PUT /DynamicsNAV/WS/CRONUS_AG/Codeunit/Letters/ HTTP/1.1[\r][\n]"
    2009/01/28 11:31:47:546 CET [DEBUG] HttpMethodBase - Adding Host request header
    2009/01/28 11:31:47:562 CET [DEBUG] header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - >> "Host: XXX:7047[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - >> "Content-Length: 0[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - >> "[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] EntityEnclosingMethod - Request body has not been specified
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "HTTP/1.1 401 Unauthorized[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "HTTP/1.1 401 Unauthorized[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "Content-Length: 0[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "Server: Microsoft-HTTPAPI/1.0[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "WWW-Authenticate: Negotiate[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "Date: Wed, 28 Jan 2009 10:31:47 GMT[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] header - << "[\r][\n]"
    2009/01/28 11:31:47:562 CET [DEBUG] HttpMethodDirector - Authorization required
    2009/01/28 11:31:47:578 CET [DEBUG] AuthChallengeProcessor - Supported authentication schemes in the order of preference: [ntlm, digest, basic]
    2009/01/28 11:31:47:578 CET [DEBUG] AuthChallengeProcessor - Challenge for ntlm authentication scheme not available
    2009/01/28 11:31:47:578 CET [DEBUG] AuthChallengeProcessor - Challenge for digest authentication scheme not available
    2009/01/28 11:31:47:578 CET [DEBUG] AuthChallengeProcessor - Challenge for basic authentication scheme not available
    2009/01/28 11:31:47:578 CET [WARN] HttpMethodDirector - Unable to respond to any of these challenges: {negotiate=Negotiate}
    401
    Stat=Unauthorized
    2009/01/28 11:31:47:578 CET [DEBUG] HttpMethodBase - Buffering response body
    2009/01/28 11:31:47:578 CET [DEBUG] HttpMethodBase - Resorting to protocol version default close connection policy
    2009/01/28 11:31:47:578 CET [DEBUG] HttpMethodBase - Should NOT close connection, using HTTP/1.1
    2009/01/28 11:31:47:578 CET [DEBUG] HttpConnection - Releasing connection back to connection manager.
    2009/01/28 11:31:47:578 CET [DEBUG] HttpMethodBase - Default charset used: ISO-8859-1
    
  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    Try to post the source code too - thanks
    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.
  • hans51hans51 Member Posts: 5
    My Sourcecode:

    Main:
    package client.ws;
    
    
    
    
    import java.net.HttpURLConnection;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    import java.net.URLConnection;
    
    import microsoft_dynamics_schemas.LettersLocator;
    import microsoft_dynamics_schemas.Letters_Port;
    import sun.security.krb5.internal.Authenticator;
    
    
    
    
    
    public class TestNavisionWebservice {
    	
    	URL url = null;
    	String theUsername = "DEBDIDOM\\user";
    	String thePassword = "passwd";
    
    
    
    	
    	public TestNavisionWebservice() {
    	}
    	
    	private void setAuthenticator(){
    //		Authenticator.setDefault( new Authenticator() 
    //		{ 
    //		  @Override 
    //		  protected PasswordAuthentication getPasswordAuthentication() 
    //		  { 
    //		    System.out.printf( "url=%s, host=%s, ip=%s, port=%s%n", 
    //		                       getRequestingURL(), getRequestingHost(), 
    //		                       getRequestingSite(), getRequestingPort() ); 
    //		 
    //		    return new PasswordAuthentication( theUsername, thePassword.toCharArray() ); 
    //		  } 
    //		} ); 
    	}
    	
    	public void execute() {
    		try {
    			setAuthenticator();
    			//
    			String userPassword = theUsername + ":" + thePassword;
    			String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
    
    			url = new URL("http://XXXX:7047/DynamicsNAV/WS/CRONUS_AG/Codeunit/Letters");
    			URLConnection c = url.openConnection();
    			HttpURLConnection httpconn = (HttpURLConnection) c;
    			httpconn.setRequestProperty("Authorization", "NTLM " + encoding);
    			httpconn.setRequestProperty("Proxy-Authorization", "NTLM " + encoding);
    			
    			System.out.println( c.getContent() ); 
    
    			LettersLocator locator = new LettersLocator();
    			Letters_Port contract = locator.getLetters_Port();
    			System.out.println(contract.capitalize("test"));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    
    		TestNavisionWebservice test = new TestNavisionWebservice();
    		test.execute();			
    		System.exit(0);
    	}
    
    }
    
  • ara3nara3n Member Posts: 9,256
    hans51 wrote:
    My Sourcecode:

    Main:
    package client.ws;
    
    
    
    
    import java.net.HttpURLConnection;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    import java.net.URLConnection;
    
    import microsoft_dynamics_schemas.LettersLocator;
    import microsoft_dynamics_schemas.Letters_Port;
    import sun.security.krb5.internal.Authenticator;
    
    
    
    
    
    public class TestNavisionWebservice {
    	
    	URL url = null;
    	String theUsername = "DEBDIDOM\\user";
    	String thePassword = "passwd";
    
    	public TestNavisionWebservice() {
    	}
    	
    
    	public void execute() {
    		try {
    			setAuthenticator();
    			//
    			String userPassword = theUsername + ":" + thePassword;
    			String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
    
    			url = new URL("http://XXXX:7047/DynamicsNAV/WS/CRONUS_AG/Codeunit/Letters");
    			URLConnection c = url.openConnection();
    			HttpURLConnection httpconn = (HttpURLConnection) c;
    			httpconn.setRequestProperty("Authorization", "NTLM " + encoding);
    			httpconn.setRequestProperty("Proxy-Authorization", "NTLM " + encoding);
    			
    			System.out.println( c.getContent() ); 
    
    			LettersLocator locator = new LettersLocator();
    			Letters_Port contract = locator.getLetters_Port();
    			System.out.println(contract.capitalize("test"));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    
    		TestNavisionWebservice test = new TestNavisionWebservice();
    		test.execute();			
    		System.exit(0);
    	}
    
    }
    

    just shoot in the dark but have you tried

    httpconn.setRequestProperty("Authorization", "Negotiate " + encoding);
    httpconn.setRequestProperty("Proxy-Authorization", "Negotiate " + encoding);


    Also have you looked at this solution

    http://thejavamonkey.blogspot.com/2008/ ... beros.html
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • dinakarandinakaran Member Posts: 1
    how to import
    import microsoft_dynamics_schemas.LettersLocator;
    import microsoft_dynamics_schemas.Letters_Port;
    import sun.security.krb5.internal.Authenticator;

    in to eclipse ide
    iam using same code given in mysourcecode example.
Sign In or Register to comment.