Way to set credentials in HttpClient after made a wrapper for it in C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I create an interface called INetClient
which supports two clients: HttpClient
and WebClient
( I want that my app to be unit-testable)
The interface looks like:
public interface INetClient
{
...
void SetCredentials(ICredentials credentials);
void SetHeaders(Dictionary<HttpRequestHeader, string> headers);
void SetHeaders(Dictionary<string, string> headers);
...
}
For each of them, I created a wrapper class which implements that interface.
For web client, I did like:
public sealed class WebClientEx : WebClient, INetClient
{
public WebClientEx()
{
CookieContainer = new CookieContainer();
Encoding = Encoding.UTF8;
}
public void SetCredentials(ICredentials credentials)
{
Credentials = credentials;
}
}
and for http client:
public sealed class HttpClientEx : INetClient, IDisposable
{
private HttpClient _client;
public HttpClientEx()
{
var handler = new HttpClientHandler();
_client = new HttpClient(handler);
}
public void SetCredentials(ICredentials credentials)
{
// here, how I can set credentials ?
}
}
Later on I will use dependency injection like RegisterType<INetClient, WebClientEx>
or I can change easily to RegisterType<INetClient, HttpClientEx>
How can set credentials for http client without creating new instance for it ?
c#
|
show 2 more comments
I create an interface called INetClient
which supports two clients: HttpClient
and WebClient
( I want that my app to be unit-testable)
The interface looks like:
public interface INetClient
{
...
void SetCredentials(ICredentials credentials);
void SetHeaders(Dictionary<HttpRequestHeader, string> headers);
void SetHeaders(Dictionary<string, string> headers);
...
}
For each of them, I created a wrapper class which implements that interface.
For web client, I did like:
public sealed class WebClientEx : WebClient, INetClient
{
public WebClientEx()
{
CookieContainer = new CookieContainer();
Encoding = Encoding.UTF8;
}
public void SetCredentials(ICredentials credentials)
{
Credentials = credentials;
}
}
and for http client:
public sealed class HttpClientEx : INetClient, IDisposable
{
private HttpClient _client;
public HttpClientEx()
{
var handler = new HttpClientHandler();
_client = new HttpClient(handler);
}
public void SetCredentials(ICredentials credentials)
{
// here, how I can set credentials ?
}
}
Later on I will use dependency injection like RegisterType<INetClient, WebClientEx>
or I can change easily to RegisterType<INetClient, HttpClientEx>
How can set credentials for http client without creating new instance for it ?
c#
2
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
1
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I don't inherit anything fromHttpClient
, it implements two interfaces
– Snake Eyes
Nov 16 '18 at 14:41
|
show 2 more comments
I create an interface called INetClient
which supports two clients: HttpClient
and WebClient
( I want that my app to be unit-testable)
The interface looks like:
public interface INetClient
{
...
void SetCredentials(ICredentials credentials);
void SetHeaders(Dictionary<HttpRequestHeader, string> headers);
void SetHeaders(Dictionary<string, string> headers);
...
}
For each of them, I created a wrapper class which implements that interface.
For web client, I did like:
public sealed class WebClientEx : WebClient, INetClient
{
public WebClientEx()
{
CookieContainer = new CookieContainer();
Encoding = Encoding.UTF8;
}
public void SetCredentials(ICredentials credentials)
{
Credentials = credentials;
}
}
and for http client:
public sealed class HttpClientEx : INetClient, IDisposable
{
private HttpClient _client;
public HttpClientEx()
{
var handler = new HttpClientHandler();
_client = new HttpClient(handler);
}
public void SetCredentials(ICredentials credentials)
{
// here, how I can set credentials ?
}
}
Later on I will use dependency injection like RegisterType<INetClient, WebClientEx>
or I can change easily to RegisterType<INetClient, HttpClientEx>
How can set credentials for http client without creating new instance for it ?
c#
I create an interface called INetClient
which supports two clients: HttpClient
and WebClient
( I want that my app to be unit-testable)
The interface looks like:
public interface INetClient
{
...
void SetCredentials(ICredentials credentials);
void SetHeaders(Dictionary<HttpRequestHeader, string> headers);
void SetHeaders(Dictionary<string, string> headers);
...
}
For each of them, I created a wrapper class which implements that interface.
For web client, I did like:
public sealed class WebClientEx : WebClient, INetClient
{
public WebClientEx()
{
CookieContainer = new CookieContainer();
Encoding = Encoding.UTF8;
}
public void SetCredentials(ICredentials credentials)
{
Credentials = credentials;
}
}
and for http client:
public sealed class HttpClientEx : INetClient, IDisposable
{
private HttpClient _client;
public HttpClientEx()
{
var handler = new HttpClientHandler();
_client = new HttpClient(handler);
}
public void SetCredentials(ICredentials credentials)
{
// here, how I can set credentials ?
}
}
Later on I will use dependency injection like RegisterType<INetClient, WebClientEx>
or I can change easily to RegisterType<INetClient, HttpClientEx>
How can set credentials for http client without creating new instance for it ?
c#
c#
asked Nov 16 '18 at 13:59
Snake EyesSnake Eyes
8,3752685159
8,3752685159
2
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
1
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I don't inherit anything fromHttpClient
, it implements two interfaces
– Snake Eyes
Nov 16 '18 at 14:41
|
show 2 more comments
2
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
1
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I don't inherit anything fromHttpClient
, it implements two interfaces
– Snake Eyes
Nov 16 '18 at 14:41
2
2
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
1
1
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I don't inherit anything from
HttpClient
, it implements two interfaces– Snake Eyes
Nov 16 '18 at 14:41
I don't inherit anything from
HttpClient
, it implements two interfaces– Snake Eyes
Nov 16 '18 at 14:41
|
show 2 more comments
1 Answer
1
active
oldest
votes
If your web service is using HTTP Basic authentication, you could set the header using the HttpClient.DefaultRequestHeaders
property
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339322%2fway-to-set-credentials-in-httpclient-after-made-a-wrapper-for-it-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your web service is using HTTP Basic authentication, you could set the header using the HttpClient.DefaultRequestHeaders
property
add a comment |
If your web service is using HTTP Basic authentication, you could set the header using the HttpClient.DefaultRequestHeaders
property
add a comment |
If your web service is using HTTP Basic authentication, you could set the header using the HttpClient.DefaultRequestHeaders
property
If your web service is using HTTP Basic authentication, you could set the header using the HttpClient.DefaultRequestHeaders
property
answered Nov 16 '18 at 14:08
Martin WiboeMartin Wiboe
1,30512241
1,30512241
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339322%2fway-to-set-credentials-in-httpclient-after-made-a-wrapper-for-it-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
is there a reason, why you want to use 2 different web clients - which are doing pretty the same thing?
– maerlin
Nov 16 '18 at 14:02
I have two apps (one old and one mobile app where I will use httpclient because webclient is not supported) and I unify by using a core library ... and then do unit tests
– Snake Eyes
Nov 16 '18 at 14:04
1
Make HttpClientEx not inherit from HttpClient. Instead, wrap an HttpClient. You can then manage instances of HttpClient however you like. Inheritance is the wrong solution here anyway. I'd also remove the set methods and make that data an argument to the method that sends the request. No need for mutable state.
– usr
Nov 16 '18 at 14:34
I agree with @usr. You are essentially creating the Adapter pattern. An adapter doesn't inherit from the things it is adapting.
– Amy
Nov 16 '18 at 14:39
I don't inherit anything from
HttpClient
, it implements two interfaces– Snake Eyes
Nov 16 '18 at 14:41