1

I have this code both in my console app and webapi app

static void TestHitOtp()
    {
        try
        {
            string urlPOST = ConfigurationManager.AppSettings["OtpUrl"];
            string userid = ConfigurationManager.AppSettings["UserIdOtp"];
            string password = ConfigurationManager.AppSettings["PasswordOtp"];
            string phoneNumber = ConfigurationManager.AppSettings["PhoneNumberForTest"];
            string message = "message from test sms gateway from console app at " + DateTime.Now.ToString();
            var guid = Guid.NewGuid();
            var client = new RestClient(urlPOST);
            var request = new RestRequest(Method.POST);
            //ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ParameterType type = ParameterType.GetOrPost;
            request.AddParameter("userid", userid, type);
            request.AddParameter("password", password, type);
            request.AddParameter("dept", "DeptName", type);
            request.AddParameter("original", "CompanyName", type);
            request.AddParameter("sendto", phoneNumber, type);
            request.AddParameter("messageid", guid.ToString(), type);
            request.AddParameter("message", message, type);
            var fullUrl = client.BuildUri(request);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            Console.WriteLine("Test OTP From Console");
            Console.WriteLine("User Id     : "+ userid);
            Console.WriteLine("Password    :" + password);
            Console.WriteLine("Dept        : DeptName");
            Console.WriteLine("Original    : CompanyName");
            Console.WriteLine("Message Id  : "+ guid.ToString());
            Console.WriteLine("message     : "+ message);
            Console.WriteLine("Result      : "+ content);
            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
        }
        catch (Exception ex)
        {

            Console.WriteLine("Masuk Exception, Error Message = " + ex.Message);
            Console.WriteLine("Masuk Exception, Error InnerException Message = " + ex.InnerException.Message);
            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
        }
    }

i confused. i have 2 diffenrent environement production server and stagging server (server test).

but the problem is this code run well in console for both environment. but in my webapi which is this is the main app. this code just run on staging server. in production server the result of this hit just "Empty String" what i have to check for make sure these two environment have the same configuration so that these 2 environment can hit my sms gateway server. is it proxy or something if it is proxy what to configure ? web.config or what ?

and i dont know do i have to use

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

if i want to hit https url ?

update :

i think there's something blocked restsharp request so the webapi cannot hit that https url. but i don't know what to check. what block that restharp http request.

thank you

1 Answers1

0

Add this code as you mentioned in the method that is calling the API

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

This worked for me. Or you can upgrade to .Net framework 4.7 which will handle this automatically.

Hasan Shouman
  • 2,162
  • 1
  • 20
  • 26