0

I am trying to handle a response from an api, i keep getting a TLSException, and can't find any relevant informaton on this exception online.

I am posting a username and password to the server succesfully, but the exception occurs while trying to deserialize the response.

The response consists of 2 strings, and i made a class called Response, matching this. This is how i handle the response

public async Task<Response> PostResponse<Response>(string weburl, 
FormUrlEncodedContent content)
    {
        var response = await client.PostAsync(weburl, content);
        var jsonResult = response.Content.ReadAsStringAsync().Result;
        var responseObject = JsonConvert.DeserializeObject<Response> 
        (jsonResult);
        return responseObject;
    }

I followed a somewhat similar post and tried the code:

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

I dont know if this is solving my problem as this arises a new exception:

Cannot Convert System.Func[SystemObject, SystemSecurity]
Gustavbang
  • 77
  • 1
  • 1
  • 11
  • 3
    Show the full stack trace – haim770 Jul 17 '18 at 09:35
  • Check version of Newtonsoft.Json in both projects and make sure are the same. – Ruben Sebastian Jul 17 '18 at 09:37
  • first try to open post Url in address bar of your browser and tell the result – TAHA SULTAN TEMURI Jul 17 '18 at 09:40
  • Possible duplicate of [Could not establish trust relationship for SSL/TLS secure channel -- SOAP](https://stackoverflow.com/questions/703272/could-not-establish-trust-relationship-for-ssl-tls-secure-channel-soap) – TAHA SULTAN TEMURI Jul 17 '18 at 09:41
  • Is it correct to place the object that matches the response in the Task<>? – Gustavbang Jul 17 '18 at 09:42
  • 2
    A TLS exception should have nothing to do with `Json.NET`, do you even get a `jsonResult`? Also why are you using `.Result` when you can await? – Lloyd Jul 17 '18 at 09:42
  • I am not sure if i even get a jsonresult, if i change the ip to something else the exception doesn't occur, so that tells me i am receiving atleast something? – Gustavbang Jul 17 '18 at 09:45
  • TLS exception is not to do with deserialisation. TLS is Transport Layer Security, like HTTPS. – Stevo Jul 17 '18 at 09:45
  • var jsonResult = await response.Content.ReadAsStringAsync(); like that? – Gustavbang Jul 17 '18 at 09:46
  • @haim770 no stack trace, just a pop up from xamarin live on my device that says: Uncaught Exception, Certificate Unknown(TlsException) – Gustavbang Jul 17 '18 at 09:47
  • Yeah @SteveJ, could that mean that i don't authenticate to the server using my current username / password? It should be correct. – Gustavbang Jul 17 '18 at 09:48
  • 1
    Possible duplicate of [HTTPS Post failure](https://stackoverflow.com/questions/48450290/https-post-failure) – phuzi Jul 17 '18 at 09:49
  • Can someone answer me though, the Task<> should have the object that corresponds to the response right? – Gustavbang Jul 17 '18 at 09:53
  • 2
    @Gustavbang not sure. I wouldn't have thought so. Can you query the API with a browser, PostMan or Fiddler? See if you get certificate errors doing that. Does the API endpoint start with http: or https:? – Stevo Jul 17 '18 at 09:54
  • "the Task<> should have the object that corresponds to the response "...if you're getting a TLS Exception its failing long before you get any kind of response. It's not even making a valid connection to the server. Your client does not think the server's SSL certificate is valid. – ADyson Jul 17 '18 at 10:43
  • I see, thanks for clarification! The connection is from Huawei and i know another programmer succeeded in connection, can something be done from client side to accept the certificate? – Gustavbang Jul 17 '18 at 10:49
  • @TAHASULTANTEMURI When i try to open in my browser it says: HTTP method GET is not supported by this URL This tells me that the connection should be open.. I really dont know where to look for this error – Gustavbang Jul 17 '18 at 11:00
  • Skip using Xamarin Live, deploy/debug directly on the device and add the Exception/Stacktrace to your question – SushiHangover Jul 17 '18 at 11:16

0 Answers0