Sending HttpWebRequest with a Unicode string
-
Hello. I'm implementing WebDAV COPY method.
public void Copy(string userName, string password, string source, string destination)
{
CredentialCache MyCredentialCache = new CredentialCache();
MyCredentialCache.Add(new System.Uri(source), "Basic", new NetworkCredential(userName, password));try { HttpWebRequest Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(source); Request.Credentials = MyCredentialCache; Request.Method = "COPY"; Request.Headers.Add("Destination", destination); Request.Headers.Add("Overwrite", "F"); HttpWebResponse Response = (System.Net.HttpWebResponse)Request.GetResponse(); Response.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }
The method works fine with non-unicode file names. But, if I want to copy a file with korean letters in its name (http://myDAVserver.myCompany.com/dav/한글.txt) I get the following error:
Message: Specified value has invalid Control characters.
Parameter name: valueI tracked the error, and the exception is being throwed exactly in
Request.Headers.Add("Destination", destination);
Does anybody have an idea how to handle this problem? Thank you.
-
Hello. I'm implementing WebDAV COPY method.
public void Copy(string userName, string password, string source, string destination)
{
CredentialCache MyCredentialCache = new CredentialCache();
MyCredentialCache.Add(new System.Uri(source), "Basic", new NetworkCredential(userName, password));try { HttpWebRequest Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(source); Request.Credentials = MyCredentialCache; Request.Method = "COPY"; Request.Headers.Add("Destination", destination); Request.Headers.Add("Overwrite", "F"); HttpWebResponse Response = (System.Net.HttpWebResponse)Request.GetResponse(); Response.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }
The method works fine with non-unicode file names. But, if I want to copy a file with korean letters in its name (http://myDAVserver.myCompany.com/dav/한글.txt) I get the following error:
Message: Specified value has invalid Control characters.
Parameter name: valueI tracked the error, and the exception is being throwed exactly in
Request.Headers.Add("Destination", destination);
Does anybody have an idea how to handle this problem? Thank you.
The problem could be a conflict between the character sets allowed in URIs and filenames (on whatever system). I had a quick peek at the RFCs but the allowed character set for URIs didn't leap out at me. (The destination parameter is specified by the WebDAV RFC as being a URI.) Perhaps the documentation of HttpWebRequest specifies what character set it will allow in headers - it should, if it throws an exception when you get it wrong. :) Not a solution on a plate, but hopefully this will point you in the right direction. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Hello. I'm implementing WebDAV COPY method.
public void Copy(string userName, string password, string source, string destination)
{
CredentialCache MyCredentialCache = new CredentialCache();
MyCredentialCache.Add(new System.Uri(source), "Basic", new NetworkCredential(userName, password));try { HttpWebRequest Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(source); Request.Credentials = MyCredentialCache; Request.Method = "COPY"; Request.Headers.Add("Destination", destination); Request.Headers.Add("Overwrite", "F"); HttpWebResponse Response = (System.Net.HttpWebResponse)Request.GetResponse(); Response.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }
The method works fine with non-unicode file names. But, if I want to copy a file with korean letters in its name (http://myDAVserver.myCompany.com/dav/한글.txt) I get the following error:
Message: Specified value has invalid Control characters.
Parameter name: valueI tracked the error, and the exception is being throwed exactly in
Request.Headers.Add("Destination", destination);
Does anybody have an idea how to handle this problem? Thank you.
-
HTTP headers have to be 7 bit ASCII, I think, nominally. They are certainly interpreted at best as 8 bit ANSI (Windows western encoding). You will need to encode the Unicode characters.
Thank you for the answer. Yes. You are right. I could have encoded the Unicode characters and URL using HttpUtility.UrlEncode Method. But the problem is, it is implemented only in .Net 4.0, but unfortunately, I'm writing my program in .Net 3.5 do to the compatibility with "a little bit old OSs" with older versions of .Net. Still did not find a proper solution. That was totally wrong. I tried to use HttpUtility.UrlPathEncode(str) method directly without adding reference to the System.Web Namespace in project's properties:
...
using System.Web;
...
string convertedPath=HttpUtility.UrlPathEncode(path);
...And after adding reference, I did like this:
...
string convertedPath=System.Web.HttpUtility.UrlPathEncode(path);
...and it worked. Thank you guys!