Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

.net - How to authenticate against a proxy when using the HttpClient class?

I have a console app built on .NET 4 that uses the HttpClient library (obtained via NuGet) to retrieve data from a public API over the internet. The console app sits behind a proxy. The Windows machine it sits on has the correct proxy settings in Internet Explorer. When the console application attempts to access the outside world it is given a 407 status - "proxy authentication required".

I've written no specific code to deal with a proxy yet. I'm assuming from the 407 error that the application is directing to the proxy no problem, but the proxy isn't authenticating the request.

My question is, is it possible to set the HttpClient to use the credentials of the logged in user to authenticate the proxy? If so, how?

var client = new HttpClient(webRequestHandler);
client.PostAsync(RequestUri, MyContent);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to solve my problem very simply through proxy configuration in app.config.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy usesystemdefault="True" />
    </defaultProxy>
</system.net>

http://msdn.microsoft.com/en-us/library/dkwyc043.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...