HTTPS Proxy failing because of ServicePointManager - c#

I'm trying to connect through a https proxy but an exception is always caught stating that "The ServicePointManager does not support proxies of https scheme".
The code I'm using to test the connection is the following one :
var handler = new HttpClientHandler
{
UseProxy = true,
Proxy = new WebProxy("https://xxx.xxx.xxx.xxx:443") {Credentials = new NetworkCredential(Username, Password)}
};
using (var client = new HttpClient(handler))
{
var x = await client.GetStringAsync("https://api.ipify.org/?format=text");
}
I also tested the proxy using curl and it worked perfectly (although I had to use --proxy-insecure for some reason). The command I used is :
curl --proxy-insecure -x https://username:password#xxx.xxx.xxx.xxx:443 https://api.ipify.org/?format=text
When I tried using http:// for the proxy curl just gave me the error "(56) Proxy CONNECT aborted".
So, is it possible to use a https proxy with the .NET Framework ? (I'm currently using .NET 4.6.2)

Related

c# httpclient - disable ntlm

I'm using this code to connect to a third party server.
using (HttpClientHandler httpClientHandler = new HttpClientHandler())
{
httpClientHandler.AllowAutoRedirect = false;
httpClientHandler.Credentials = new NetworkCredential(login, password);
using (HttpClient authClient = new HttpClient(httpClientHandler))
{
response = await authClient.GetAsync(authenticationUrl).ConfigureAwait(false);
... response processing here
}
}
The third party server is an appliance, and they've turned on NTLM recently. Starting with the turning on of NTLM, my request now gets an HTTP 500 error error like this:
type Exception report message NTLM specified. Downgraded to Basic
Auth (and/or SSL) but downgrade not supported. description The server
encountered an internal error that prevented it from fulfilling this
request. exception java.lang.UnsupportedOperationException: NTLM
specified. Downgraded to Basic Auth (and/or SSL) but downgrade not
supported.
net.sourceforge.spnego.SpnegoProvider.negotiate(SpnegoProvider.java:146)
net.sourceforge.spnego.SpnegoAuthenticator.authenticate(SpnegoAuthenticator.java:271)
net.sourceforge.spnego.SpnegoHttpFilter.doFilter(SpnegoHttpFilter.java:229)
I'm assuming my httpclient sees that the server now supports NTLM and tries to do NTLM. Is there any way to tell my httpclient to don't even bother with NTLM?
To disable NTLM try this:
var modules = AuthenticationManager.RegisteredModules;
while (modules.MoveNext())
{
var module = (IAuthenticationModule) modules.Current;
if (module.AuthenticationType == "NTLM")
{
AuthenticationManager.Unregister(module);
break;
}
}

How to do NTLM Authentication with Xamarin ModernHttpClient or System.Net.Http.httpClient

I am using this httpClient in a Xamarin application:
var httpClient = new HttpClient(new NativeMessageHandler());
My server requires NTLM authentication. I believe I have to do something like this:
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("NTLM",...);
But it is unclear to me, how I should replace the "...". The Documentation fo AuthenticationHeaderValue does not say anything.
Is this the correct way? What do I have to do?
For Android/iOS/UWP in a .NET Standard 1.4 library using System.Net.Http.httpClient used in a Xamarin project: (I have not used ModernHttpClient yet)
// Note: The NTLM domain is important here, otherwise basic auth will be used:
var credentials = new NetworkCredential("username", "password", "domain");
var handler = new HttpClientHandler { Credentials = credentials, UseDefaultCredentials = false }
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Authorization can be left alone, as the above code will internally generate the authorization header for each request.

C# WebProxy not works with https

I have Client + Win service.
Both must work with web server using proxy.
Without proxy all works fine,
With system proxy settings (proxy settings from ie)
WebRequest.DefaultWebProxy
Client works fine, but service don't see this proxy settings (netsh winhttp set proxy don't help me). So - proxy server works OK
When I trying to use manual settings:
var_proxy = new WebProxy(Server + ":" + Port, true)
{Credentials = null};
var request = (HttpWebRequest)WebRequest.Create(target);
request.ContentType = Constants.ContentType; // Default content type
request.UserAgent = _userAgentHeader;
request.Method = "POST";
request.Proxy = _proxy;
And as I see in proxy logs - It works with http server, but not used with https! All requests go directly.
How can I fix it?

HttpWebResponse and Proxy w/ NetworkCredentials Not Working

I am working on a project that uses proxy to obtain websites' HTML code.
Now, the trouble I have is that I want to use Username-Password authentication and not IP-Auth when connecting to the proxy.
I have written a sample code and ran it with Snippy. It worked. Then I copied the same code into a Visual Studio .NET 4.5 Project and it failed with the error: An existing connection was forcibly closed by the remote host when trying to get the response.
Here is the code:
WebProxy[] proxies = { new WebProxy("ip", port) };
proxies[0].Credentials = new NetworkCredential { UserName = "username", Password = "password" };
string url = "https://www.google.com/search?q=s&num=50";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = proxies[0];
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader response_stream = new StreamReader(res.GetResponseStream())
{
string html = response_stream.ReadToEnd();
Console.WriteLine(html);
}
}
I have tried different variations. When I switch to authorization by IP, add mine and comment out the NetworkCredentials assignment, the code works perfectly both in Snippy and Visual Studio.
But why does it fail when using NetworkCredentials?
OK, I found the culprit. It was my proxy provider.
Note for everybody ever running into problems when testing proxy connection with HttpWebRequest -- try another seller. It might save you a couple of minutes. Or hours.

WebRequest with Proxy to whatsmyip.net shows my real ip

I'm trying to proxy my requests, but it seems that the proxy setting is ignored.
I'm using the following code:
var req = (HttpWebRequest)WebRequest.Create("http://whatsmyip.net/");
req.Proxy = new WebProxy("195.128.253.243", 8080) { BypassProxyOnLocal = false };
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
var html = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
The proxy is just a random free proxy from here.
The result always contains my real ip instead of the proxy ip.
When I'm surfing to that website using hidemyass or other alternatives, the ip changes as expected.
Anyone has an idea for what am I doing wrong?
I just tried your code (without the HttpWebRequest cast) bouncing off my local tor server and it works as expected. Have you tried the proxy directly within IE?

Categories

Resources