cant consume NAV Web Service in C#.

MajesticMajestic Member Posts: 10
edited 2013-09-30 in NAV Three Tier
IM new on this forum, so bare with me if i posted this in the wrong forum.

Here goes...

I have been trying all sorts of ways to consume this web-service i have been given. Ill try and show you what i do to get where i am atm.

1. i start by adding the service reference in VS. everything goes as intended, i can see all the methods given to me.
2. to access the methods, and hopefully get some data, i do the following:
BMGWS ws = new BMGWS();

            Vogn vogn = new Vogn();
            VognType ty = new VognType();

            NetworkCredential cred = new NetworkCredential("username","Password","domain");
            ws.PreAuthenticate = true;
            ws.Credentials= cred;

            ws.SendVogn("BMG 2013", true, ref vogn);

            foreach (VognType c in vogn.Vogn1)
            {
                Console.WriteLine(c.RegNr);
            }

i get the following error: HTTP-status 401: Unauthorized.

I can say that the service i consume have a bunch of methods, only one method return a string, rest return void. the method that returns a string is working perfectly, but the "void" methods are not, im abit confused about that, so if anyone could shet some light on this i would get really happy.

any help would be much apreciated, since im kinda stuck

regards Majestic

Answers

  • Wisa123Wisa123 Member Posts: 308
    Did you expose your Webservice correctly? Can you access it in IE?

    How does the URL look like that you added as Web-Reference?
    Do you mind just trying ws.usedefaultcredentials = true?
    Austrian NAV/BC Dev
  • MajesticMajestic Member Posts: 10
    Wisa123 wrote:
    Did you expose your Webservice correctly? Can you access it in IE?

    How does the URL look like that you added as Web-Reference?
    Do you mind just trying ws.usedefaultcredentials = true?


    i added a webservice by right clicking on my solution and chose "add webservice", and clicking advanced and adding a web referernce.

    thereafter i get alot of services/methods added that i can use, as i explain above, there is only one method that sends somthing in return, my checkLogin method gives me a string in return if its a correct login that has been parsed. the other methods returns void eg nothing, that is confuesing to me.

    Anyhow i tried what you said about the "ws.usedefaultcredentials = true" and it seems like it works with my login method. but does still not work with the rest of the methods.

    my code atm looks like this:
    ws = new BMGWS();
                Vogn vogn = new Vogn();
                ws.UseDefaultCredentials = true;
                ws.Url = "http:// adress:port/BMGLIGHT2013/WS/Admin/Codeunit/BMGWS";
    
                string s = ws.CheckLogin("string", "string", int);
    
                Console.WriteLine(s);
    
                ws.SendVogn("CRONUS AS", true, ref vogn);
    
                foreach (VognType c in vogn.Vogn1)
                {
                    Console.WriteLine(c.RegNr.ToString());
                }
    
                Console.ReadLine();
    

    my checklogin() works well, tho my ws.sendvogn() does not :-k - Its supposed to send me some 3 tables of data from NAV

    At the moment i dont get any errors when building, but i get absolutly nothing in return from NAV.

    If i add the service without adding it as a web reference, but as a normal Service refernce, i cant get my loginmethod to work... i get the error: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM’

    i use this code for my normal service reference:
    BMGWS_PortClient ws = new BMGWS_PortClient();
                Vogn vogn = new Vogn();
    
                NetworkCredential cred = new NetworkCredential("Username", "Password", "domain");
                ws.ClientCredentials.Windows.ClientCredential = cred;
    
                string s = ws.CheckLogin("string", "string", int);
    
                Console.WriteLine(s);
    

    hope you can help!
  • MajesticMajestic Member Posts: 10
    and yes, i can access it in internet explore

    here is the XML of the SendVogn:
    <element name="SendVogn">
    <complexType>
    <sequence>
    <element minOccurs="1" maxOccurs="1" name="comp" type="string"/>
    <element minOccurs="1" maxOccurs="1" name="inaktiv" type="boolean"/>
    <element minOccurs="1" maxOccurs="1" name="bMGVogn" type="q4:Vogn"/>
    </sequence>
    </complexType>
    </element>
    
  • MajesticMajestic Member Posts: 10
    I have solvedmy problem! the fix was:
    ws.SendVogn("BMG 2013", true, ref vogn);
    

    there was no test data in the database with the Bool value true, changed it to false and it worked, my working code looks like this:
    BMGWS ws = new BMGWS();
                Vogn vogn = new Vogn();
                
                ws.UseDefaultCredentials = true;
    
                Console.ReadKey();
                ws.SendVogn("BMG 2013", false, ref vogn);
    
                foreach (VognType c in vogn.Vogn1)
                {
                    Console.WriteLine();
                    Console.WriteLine(c.Beskrivelse);
                    Console.WriteLine(c.Kode);
                    Console.WriteLine(c.RegNr);
                    Console.WriteLine(c.Spaerret);
                    Console.WriteLine();
                }
    
    

    it works perfectly!
Sign In or Register to comment.