0

I would like to get response from a rest API and then print as part of my testing project.

public class MyClass {
  
  public static void CreateObject() {
    string latestVersion;
    string testStepDescription = "API_TEST: ";
    string testStepIdentifier = "TestStep";

    string uri = string.Format("https://MyAPI.Test/endpoint");
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("user:12345")); //userID amd Password
    request.ContentType = "application/json";

    try {

      WebResponse webResponse = request.GetResponse();
      Stream webStream = webResponse.GetResponseStream();
      StreamReader responseReader = new StreamReader(webStream);
      latestVersion = responseReader.ReadToEnd();
      responseReader.Close();
    

      Output.WriteLine("RESULT = "+ latestVersion);
      Report.TestStepPass(testStepIdentifier, testStepDescription);
    }


    catch {
      latestVersion = "Not succeeded";
      Output.WriteLine("RESULT "+ latestVersion); //trying to see when the code goes to catch block
      Output.WriteLine("RESULT = Nooooo");
      Report.TestStepFail(testStepIdentifier, testStepDescription);
    }
  }

The code always goes to catch block and does not execute the try {}.
Would be grateful if anybody can help

I'm excepting to output the content from the API endpoint after using the credentials for authentication.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 4
    You get an exception object in the catch block that should tell you what the problem is. Just don't ignore it. – Ralf Nov 06 '22 at 12:28
  • You are using HTTPS which uses TLS to establish an encryption mode. TLS is sent before the request is transmitted. So TLS is failing when you execute WebResponse webResponse = request.GetResponse(); – jdweng Nov 06 '22 at 12:33
  • Yes I have tried that too. but still can't see where is the problem. Getting this exception actually: " System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel at System.Net.HttpWebRequest.GetResponse() at line 51 (Line: WebResponse webResponse = request.GetResponse(); )" – Andreas Nov 06 '22 at 12:42
  • @jdweng do you have any suggestions as a fix please ? – Andreas Nov 06 '22 at 12:44
  • You need to check the response. So define the response before the try using : WebResponse webResponse = null; Than in catch put break point and check the weResponse Status which should be 200 OK if good. If you get a response than TLS passes and you should get an error usually a 400 or 500 error. The error response should indicate if the credentials passed or failed. You may get a 403 indicating the credentials failed or an error indicating the server failed while processing the request. – jdweng Nov 06 '22 at 12:53
  • this can resolve your issue [The request was aborted: Could not create SSL/TLS secure channel](https://stackoverflow.com/questions/10822509/the-request-was-aborted-could-not-create-ssl-tls-secure-channel) – Pradeep Kumar Nov 07 '22 at 06:25

1 Answers1

0

Try this One

Enable other security protocol versions to resolve the issue:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
        | SecurityProtocolType.Tls11
        | SecurityProtocolType.Tls12
        | SecurityProtocolType.Ssl3;

OR

 // You must change the URL to point to your Web server.
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "GET";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;


        // Skip validation of SSL/TLS certificate
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


        WebResponse respon = req.GetResponse();
        Stream res = respon.GetResponseStream();

        string ret = "";
        byte[] buffer = new byte[1048];
        int read = 0;
        while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
        {
            Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
            ret += Encoding.ASCII.GetString(buffer, 0, read);
        }
        return ret;

For more detail Please click the below link

The request was aborted: Could not create SSL/TLS secure channel

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21