Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:46:33+00:00 2026-06-13T08:46:33+00:00

I am using HttpClient (aka Web API client) to consume RESTfull services. Services require

  • 0

I am using HttpClient (aka Web API client) to consume RESTfull services.
Services require session to be established (via login) and then destroyed (via logout) upon each operation. So the call to consume service A looks something like this (pseudocode)

// setup
create auth dictionary authDict
create authenticationContent using FormUrlEndodeContent(authDict)
create cookieContainer
create HttpClientHandler...
create HttpClient

// login
await httpClient.PostAsync(LoginUrl, authenticationContent);
do error checking

// perform Operation A
await httpClient.....post...or...get...
extract data, process it, tranform it, get a cup of coffee, etc, etc
populate OperationAResult

// logout
await httpClient.GetAsync(LogoutUrl);

// return result
return OperationAResult

My question is, how can I easily reuse setup, login, and logout for different operations?
Should I be creating some method that will take in Action<> and if so how do I make sure that operations occur in order?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:46:34+00:00Added an answer on June 13, 2026 at 8:46 am

    Probably the easiest way is to just write a wrapper class.

    public class MyHttpClient
    {
      private HttpClient _client = new HttpClient();
      private MyHttpClientSetup _setup;
    
      public MyHttpClient(MyHttpClientSetup setup)
      {
        this._setup = setup;
      }
    
      private void HttpLogin() 
      { 
        // .. custom login stuff that uses this._setup
      }     
    
      private void HttpLogout() 
      { 
        // .. custom logout stuff that uses this._setup
      }     
    
      public void Reset()
      {
        this._client = new HttpClient();
      }
    
      // Wrapped Properties from the private HttpClient (1 example)
      public Uri BaseAddress 
      { 
        get{ return this._client.BaseAddress;} 
        set{ this._client.BaseAddress = value;} 
      }
    
      // Wrapped HttpMethods (1 example)
      // Extremely poorly written, should be delegated properly
      // This is just a bad example not using Task properly
      public Task<HttpResponseMessage> DeleteAsync(string requestUri)
      {
        this.HttpLogin();
        Task<HttpResponseMessage> result = this._client.DeleteAsync(requestUri);
        this.HttpLogout();
        return result;
      }
    
    
      public class MyHttpClientSetup
      {
        // Properties required for setup;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using HttpClient 3.1 to connect to a different web services. I have
I am invoking ASP .Net Web API using HttpClient and invoke actions successfully. Also
I'm using HttpClient to post then retrieve something in Android, which works great, but
I'm reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget
I'm using Apache HttpClient 3.x for contacting a Big IP that will then redirect
I am using Apache HTTPClient 4 to connect to twitter's streaming api with default
I'm using HttpClient 0.6.0 from NuGet. I have the following C# code: var client
I am using HttpClient 4.02 to create a connection via proxy (using the CONNECT
We're using HTTPClient to implement a REST API. We're reading the server response using:
I'm using an API client that is completely asynchrounous, that is, each operation either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.