Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sending HttpWebRequest with a Unicode string

Sending HttpWebRequest with a Unicode string

Scheduled Pinned Locked Moved C#
helpcsharpcomtutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShadowUz
    wrote on last edited by
    #1

    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: value

    I 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.

    P B 2 Replies Last reply
    0
    • S ShadowUz

      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: value

      I 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.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S ShadowUz

        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: value

        I 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.

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • B BobJanova

          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.

          S Offline
          S Offline
          ShadowUz
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups