Search code examples
c#webclientwebclient.uploaddata

WebClient UploadData fails with SSL/TLS Error


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.


Solution

  • 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