Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

We have done integrated our application with one third party. We are consuming their Rest API developed in Java from C# application.

They have disabled all the protocols in the server other than TSL1.2(i.e. SSL2, TSL1.0, TSL1.1)

When we call the Rest API we are getting the error. Please find below my code

What I have tried:

string response;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls|SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback = delegate(object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
        WebClient client = new WebClient();
        string credentials;
      
        credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("gujarat_ws_client" + ":" + "guj@r@t_C|ient"));
        client.Headers.Add("Authorization", "Basic Z3VqYXJhdF93c19jbGllbnQ6Z3VqQHJAdF9DfGllbnQ=");                                             
        try
        {
            
            var strEaushadhiURI = "https://xxx.com/";

            System.Net.ServicePointManager.ServerCertificateValidationCallback = (object se, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslerror) => true;
            response = client.DownloadString(new Uri(strEaushadhiURI + "/AMH/" + txtVoucherNumber.Text.Trim()));
            System.Net.ServicePointManager.ServerCertificateValidationCallback = null;
            TextBox1.Text = response;
        }
        catch (Exception ex)
        {
            TextBox1.Text = ex.Message;
        }


But I am getting error :
The remote server returned an error: (401) Unauthorized.
Posted
Updated 31-Jul-18 18:34pm

The "certificate" is to insure the server is "valid / secure"; it has nothing to do with your "authority" to gain access to said server.

You are simply not "signing in" properly; implying an "account and password" or security token is required for access.
 
Share this answer
 
I had the same problem and it was because of my request headers.
I will put a sample code for you :

httpWReq.UserAgent = "Apache-HttpClient/4.2.1";
httpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
httpWReq.ContentType = "text/plain";
httpWReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
httpWReq.Headers.Add("Signature", CanonicalReq);
httpWReq.Headers.Add("SignedHeaders", "host");
/* httpWReq.Headers.Add("content-Type", "text/plain")*/
//user agent
httpWReq.ProtocolVersion = HttpVersion.Version11;
httpWReq.Method = "GET";


All the best.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900