1

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.

  • Add C# code that you tried. It will be easier to help you then – PiotrWolkowski Jun 29 '14 at 02:17
  • Is the HTTPS the problem or the Get? If you can test a HTTP get fine then S is your problem. Just so we know which direction your having problems with. – Simeon Pilgrim Jun 29 '14 at 02:39
  • This may help: http://stackoverflow.com/questions/22251689/make-https-call-using-httpclient – Yuval Itzchakov Jun 29 '14 at 03:18
  • I think is not required to send a certificate from the client. I don't know if it is the same certificate I've downloaded when I browse the Service, or not. In java is not required. Any ideas about the problem with the authentication? – user3786959 Jul 02 '14 at 17:47
  • The solution was adding the header manually in the request :) . http://stackoverflow.com/questions/1702426/httpwebrequest-not-passing-credentials – user3786959 Jul 03 '14 at 16:23

0 Answers0