1

I'm working in a project that needs to upload a file into a web application using WebClient.

I tried with the following code but the server doesn't recognize the special characters defined in the slug header and replaces them with other not printable characters.

WebClient.Headers.Clear();
WebClient.Headers.Add("Content-Type",GetMimeType(Path.GetExtension("aáñÑ.pdf")));
WebClient.Headers.Add("Accept", "*/*");
WebClient.Headers.Add("Referer", myRefererURL);
WebClient.Headers.Add("x-csrf-token", "securityTokenFromModel");
WebClient.Headers.Add("slug", "aáñÑ.pdf");

Also, after reading rfc2047 (http://www.ietf.org/rfc/rfc2047.txt) I replaced the last line with the following code, but server doesn't recognize the request and returns an error.

WebClient.Headers.Add("slug", "(=?ISO-8859-1?q?" + "aáñÑ.pdf" + "?=)");

Is there another way to set the enconding charset to allow using special characters (accents, spanish characters) in the slug header?

Edit:

After reading @Julian answer, I tried to change the slug header to look like this:

WebClient.Headers.Add("slug", "The Beach at S%C3%A8te");

But the web application sets the filename exactly: "The Beach at S%C3%A8te".

In another test, this is how Fiddler shows the request using filename "Documentación Ññ.docx":

Request made by Internet Explorer 11: OK

Request made by Internet Explorer 11: OK

Request made by .NET WebClient and Google Chrome: ERROR

enter image description here

Frank_FC
  • 76
  • 1
  • 8
  • Have you tried to escape the slug value by `System.Web.HttpUtility.HtmlEncode()`? I mean something like `WebClient.Headers.Add("slug", "(=?ISO-8859-1?q?" + System.Web.HttpUtility.HtmlEncode("aáñÑ.pdf") + "?=)");` this. – Ali Bahrami Nov 24 '16 at 15:54
  • Thanks @Ali but it didn't work. Using WebClient.Headers.Add("slug", "(=?ISO-8859-1?q?" + System.Web.HttpUtility.HtmlEncode("aáñÑ.pdf") + "?=)"); returns an error from the server and using WebClient.Headers.Add("slug", "(System.Web.HttpUtility.HtmlEncode("aáñÑ.pdf")); sets the filename with encoded characters. – Frank_FC Nov 24 '16 at 16:16
  • I think you need to encode the file name into `ISO-8859-1` before sending. Check this: http://stackoverflow.com/questions/629617/how-to-convert-string-to-iso-8859-1 – Ali Bahrami Nov 24 '16 at 16:21
  • encoding the filename returns an array of bytes, do you know if this array can be sent into the header? – Frank_FC Nov 24 '16 at 16:32
  • Forget about what I said about encoding the header values. Have you tried to set webClient `Encoding`? Something like `client.Encoding = UTF8Encoding.UTF8;` And don't forget to remove `(=?ISO-8859-1?q?`. – Ali Bahrami Nov 24 '16 at 17:04
  • @Ali Yes, I also tried setting webclient encoding to UTF8 but without success. Thanks. – Frank_FC Nov 25 '16 at 10:33

1 Answers1

1

The answer is in the specification:

"The field value is the percent-encoded value of the UTF-8 encoding of the character sequence to be included (see Section 2.1 of [RFC3986] for the definition of percent encoding, and [RFC3629] for the definition of the UTF-8 encoding).

Implementation note: to produce the field value from a character sequence, first encode it using the UTF-8 encoding, then encode all octets outside the ranges %20-24 and %26-7E using percent encoding (%25 is the ASCII encoding of "%", thus it needs to be escaped). To consume the field value, first reverse the percent encoding, then run the resulting octet sequence through a UTF-8 decoding process."

https://greenbytes.de/tech/webdav/rfc5023.html#rfc.section.9.7.1

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • Thanks for your answer. I edited the question to show results of another tests I made. – Frank_FC Nov 25 '16 at 10:27
  • When you say "sets the filename", what are you referring to? The URI of the created resource? In which case the server would be working exactly they way it should. – Julian Reschke Nov 25 '16 at 13:50
  • What I meant is that the uploaded file in the server gets "The Beach at S%C3%A8te" as its filename (with the percent signs and numbers) and I think these should be replaced by their corresponding special characters in latin charset. – Frank_FC Nov 28 '16 at 08:33
  • From an HTTP point of view, "filename" is irrelevant. What is the *URI* of the created resource? – Julian Reschke Nov 28 '16 at 10:43
  • I don't have direct access to the file URI. To download the file I need to make a request to an URL (I'm trying to replicate user interaction with a web app, I don't request a REST service directly). URL of a file uploaded correctly with IE: `http://sd1.sd2.pre.corp:8080/archive?get&docId=A38C3B5801EF8397E1000000B45D&compId=Documentaci%25C3%25B3nde%25C3%2591%25C3%25B1.docx` and the URL of a file uploaded incorrectly with Webclient : `http:/sd1.sd2.pre.corp:8080/archive?get&docId=FF8F3B5801EF8397E1000000B45D&compId=Documentaci%25EF%25BF%25BDnde%25EF%25BF%25BD%25EF%25BF%25BD.docx` – Frank_FC Nov 28 '16 at 14:48
  • URI == URL. So yes, it seems like the header is processing the Slug header field incorrectly. – Julian Reschke Nov 28 '16 at 14:57