0

I'm using the System.Net.WebClient class (https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx) to communicate with a REST API of a server. I'm able to do most operations without problems, but I can't get the "UploadData" method to work (https://msdn.microsoft.com/en-us/library/ms144221(v=vs.110).aspx). When I call that method I get the error "Could not create SSL/TLS secure channel." I have no problem running the corresponding "UploadString" method though. Does anyone have any idea what might be wrong?

To summarize, this method works:

public string Post(string endpoint, string body)
{
    return UploadString(new Uri(Uri, endpoint), body);
}

But this fails:

public byte[] Post(string endpoint, byte[] body)
{
    return UploadData(new Uri(Uri, endpoint), body);
}

I've been experimenting a lot with different header settings for the resulting request, but haven't been able to make it work. And I'm rather confused that it complains about that SSL/TLS channel problem and don't see how the two calls differs with respect to that.

Øystein Kolsrud
  • 359
  • 2
  • 16
  • 1
    Debug your request somehow (WireShark or Fiddler). There is probably another error hidden behind "Could not create SSL/TLS secure channel". I have seen it popping up when server returns error 400, for example, when providing parameters in bad format. – Tewr Jan 29 '18 at 12:13
  • Related: https://stackoverflow.com/questions/37869135/is-that-possible-to-send-httpwebrequest-using-tls1-2-on-net-4-0-framework/37869237#37869237 – Crowcoder Jan 29 '18 at 13:13
  • You were right, Tewr! The parameters was indeed bad, like I described in the answer below. I used Fiddler, like you recommended, to catch it. Thanks for the tip! – Øystein Kolsrud Jan 29 '18 at 13:33

1 Answers1

0

I had done a mistake in configuring the query parameters. Apparently, one has to remove any preexisting '?' from the query when appending a new query to a UriBuilder.Query property. So I basically had to do a Substring(1) on the query like they describe here:

https://msdn.microsoft.com/en-us/library/system.uribuilder.query(v=vs.110).aspx

Øystein Kolsrud
  • 359
  • 2
  • 16