Having problems uploading PDF file via REST API using C#

I am currently writing a C# program that uses the Sharepoint REST API to manage uploading and downloading of files.

Currently can retrieve files stored in folders in a subsite without problems, but I am having issues with uploading files to the same folders.

I use “/_api/web/getfolderbyserverrelativeurl(‘/Folder1/Folder2/File)$value” to retrieve a file. This works fine.

I can also get the folder itself via “_api/web/getfolderbyserverrelativeurl(‘/Folder1/Folder2′)
I am thinking I can use the same URI structure to add a file to that folder by adding “/files/add(url=’filename’, overwrite=true)” to the end of the above URI.
Set the method to “POST”, Accept to “application/json; odata=verbose”, ConentLength to length of file being sent. Also added Authorization and X-RequestDigest headers to the webrequest.

When the webrequest is sent to the server I get the following errors back (after about 30secs).

  • HttpRequestException: An error occurred while sending the request
  • IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
  • SocketException: An existing connection was forcibly closed by the remote host

My question is… Has anyone else seen these sort of errors before and am I approaching this is the correct way.

Also related to this, once I get this file uploaded how do I add metadata to it? Or does the meta data get added to the file as it is uploaded?

Any thoughts or comments are very much appreciated.