privatestaticstring Token ="";privatestaticstring BaseURL ="https://test.imaging.wisc.edu/integrationserver/";privatestaticHttpClient client =newHttpClient();publicstaticasyncTask<string>GetSession(){//Get basic auth credentials for Integration server account from secure locationstring username = ConfigurationManager.AppSettings["IntegrationUser"];string password = ConfigurationManager.AppSettings["IntegrationPassword"];//http client data persists so first we clear headers
client.DefaultRequestHeaders.Clear();//add auth credentials to the Authorization headervar byteArray = Encoding.ASCII.GetBytes(username +":"+ password);
client.DefaultRequestHeaders.Authorization =newSystem.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));//Send the GET requestHttpResponseMessage response =await client.GetAsync(BaseURL +"connection");//throws exception if remote server does not return success code
response.EnsureSuccessStatusCode();//Token is retrieved from the response headers and saved globally
Token = response.Headers.GetValues("X-IntegrationServer-Session-Hash").FirstOrDefault();return Token;}
End Session
publicstaticasyncTask<string>CloseSession(){//http client data persists so first we clear headers
client.DefaultRequestHeaders.Clear();//Send the DELETE requestHttpResponseMessage response =await client.DeleteAsync(BaseURL +"connection");//throws exception if not success
response.EnsureSuccessStatusCode();}
Example Usage
publicasyncTask<Document>GetDocument(string DocID){//open the session await PerceptiveActions.GetSession();//Token saved to variable in PerceptiveActions class and accessed from memory//in the GetDocument methodDocument json =await PerceptiveActions.GetDocument(DocID);//close the sessionawait PerceptiveActions.CloseSession();return json;}
Keywords:
Perceptive Content Integration API Session Management