Saturday, November 24, 2018

Managing Cookies in C# (Web Services)

 

In order to manage cookies in C#, CookieContainer class must be used and reference it to NetSuiteService variable.

This is necessary when performing a search/searchMore and compiler is throwing a SoapException error: Your connection has timed out.  Please log in again.

Below is an example on how to use CookieContainer in your program:

namespace myNameSpace
{
    class JeffSuiteTalk
    {
        private NetSuiteService _ws;
        private Preferences _prefs = new Preferences();
        private CookieContainer cookieJar = new CookieContainer();

        public JeffSuiteTalk()
        { ...
        }

        static void Main(string[] args)
        {
            JeffSuiteTalk jeffSTalk = new JeffSuiteTalk();
            jeffSTalk.login();
            jeffSTalk.searchVendorPayment();
        }

        private void login()
        {
            Passport passport = new Passport();
            RecordRef roleRecordRef = new RecordRef();
            roleRecordRef.internalId = "3";

            passport.email = "jeff@nodomain.com";
            passport.password = "thepassword01";
            passport.account = "TSTDRV012345";
            passport.role = roleRecordRef;

            _ws.CookieContainer = cookieJar;
            SessionResponse sessionResponse = _ws.login(passport);
        }

        public void searchVendorPayment() {
            // Do some search here...
        }
    }
}

No comments:

Post a Comment