.NET Framework Developer Center > .NET Development Forums > Network Class Library (System.Net) > WebClient UploadData gives 400 Bad Request error when I upload file with special characters
Ask a questionAsk a question
 

QuestionWebClient UploadData gives 400 Bad Request error when I upload file with special characters

  • Friday, November 06, 2009 6:25 PMcsalle01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am trying to upload documents to a sharepoint server.  I am able to do it successfully if the file name does not have special characters. However, if for example, an & exists in the file name (file&name.txt) i get a 400 Bad Request error returned from the UploadData method.

    Here is my code:

    WebClient wcUpload = new WebClient();
    wcUpload.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    wcUpload.Encoding = System.Text.Encoding.ASCII;

    /* url is passed into my upload method */
    url = string fileName = HttpUtility.UrlEncode(url);

    try
    {
      wcUpload.UploadData(url, "PUT", fileByteArray);
    }
    catch (WebException ex)
    {
       throw;
     }

    I would really appreciate some help
    Thanks
    • Moved byJialiang Ge [MSFT]MSFT17 hours 20 minutes agomove to network class lib forum. (From:.NET Base Class Library)
    •  

All Replies

  • Monday, November 09, 2009 10:24 AMJialiang Ge [MSFT]MSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello

    Could you please print the exception message and the call stack?

    try
    {
      wcUpload.UploadData(url, "PUT", fileByteArray);
    }
    catch (WebException ex)
    {
    // Log ex.Message and ex.StackTrace here.
       throw;
    }

    Additionally, please add this code before UploadData:

    wcUpload.Headers.Add("Content-Type","application/x-www-form-urlencoded");


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, November 09, 2009 3:00 PMcsalle01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Jialiang,

    Here is the Exception Message: "The remote server returned an error: (400) Bad Request."

    Here is the stack track:

    at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
    at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
    at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
    at DocManagement.UploadDocument(Byte[] fileToUpload, string filename) line 1054"

    Here is my latest code
    WebClient wcUpload = new WebClient();
    wcUpload.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    wcUpload.Encoding = System.Text.Encoding.ASCII;
    wcUpload.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

    /* url is passed into my upload method */
    url  = "http://pathToSharePointServer/Shared Documents/" + HttpUtility.UrlEncode(filename);

    try
    {
      wcUpload.UploadData(url, "PUT", fileByteArray);
    }
    catch (WebException ex)
    {
       throw;
     }

    Still getting 400 Bad Request Error.

    Thanks
    Chris
  • 17 hours 20 minutes agoJialiang Ge [MSFT]MSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Chris,

    I currently do not see the cause of this. WebClient belongs to System.Net. We have a dedicated forum for classes in this namespace:
    Network Class Library (System.Net)
    http://social.msdn.microsoft.com/Forums/en-US/ncl/threads
    I'd like to help you move this thread. Hope that this can be answered by the experts in that forum.
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • 16 hours 41 minutes agoFeroze Daud Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you retry it without UrlEncoding the filename?

    If that doesnt work, get a tracelog (see instructions in my sig) so that we can see the request being sent over the wire.


    feroze
    --
    My blog
    Instruction on how to create a tracelog with your System.Net application