I don't know how to do a GET to a REST web service through HTTPS with C#. I know how to do it in Java, but how to get the same behavior of the below code using c#?
Java Code
int port= 443;
int protocol = "https";
AuthScope authScope = new AuthScope("host", port);
DefaultHttpClient client = new DefaultHttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pass");
TrustSelfSignedStrategy strategy = new TrustSelfSignedStrategy();
SchemeSocketFactory schemeFactory = new SSLSocketFactory(strategy);
Scheme https = new Scheme(protocol, port, schemeFactory);
client.getCredentialsProvider().setCredentials(authScope, credentials);
client.getConnectionManager().getSchemeRegistry().register(https);
HttpGet httpget = new HttpGet("https://host/url/item");
HttpResponse response = client.execute(httpget);
This is my code in c#.
Uri uri = new Uri("https://host/url/item");
WebRequest http = HttpWebRequest.Create(url);
http.Method = WebRequestMethods.Http.Get;
NetworkCredential nc = new NetworkCredential("user", "pass");
http.Credentials = nc;
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
Stream stream = response.GetResponseStream();
At First, I had the next exception: c# The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
But when I turned off the SSL Client Certificate Validation
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
Then The response has the html LOGIN page, not the xml response of the service. The url is ok, if I test it with the browser I have the right response. The Service is exposed by a Cisco Web API.
Any ideas?
Thanks for your time.