This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

C#

1 - Making Rest Calls in C#

These have been tested with .Net 4.5

Basic Auth Example

request.Headers.Authorization=newAuthenticationHeaderValue("Basic",Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes( 
    string.Format("{0}:{1}",user,password))));

Generic Request Example

publicasyncTask<T>MakeRequest<T>(Uriuri) 
{ 
     varclient=newHttpClient{Timeout=newTimeSpan(0,0,Settings.Default.timeout)}; 
     try 
     { 
          HttpResponseMessageresponse=awaitclient.GetAsync(uri); 
          if(response.IsSuccessStatusCode) 
          { 
               varcontent=awaitresponse.Content.ReadAsStringAsync(); 
               try 
               { 
                    returnJsonConvert.DeserializeObject<T>(content); 
               } 
               catch(Exceptione) 
               { 
                    thrownewException(String.Format("Error deserializing {0}, additional message: {1}",content,e.Message)); 
               } 
          } 
          else 
          { 
               thrownewException(String.Format("Error getting response from {0}, Status code: {1}",uri,response.StatusCode)); 
          } 
     } 
     catch(Exceptione) 
     { 
          log.Error(string.Format("Request timed out to: {0}",uri)); 
          throwe; 
     } 
}