locked
(C++) Upload file to C# web service with HTTP/1.1 POST

    Question

  • C# Web Service Function is declared as:

    public UploadFile(byte[] file, string FileName)

    The test form says the request should take this form:

    POST address_of_service HTTP/1.1
    Host: <host>
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length
    
    file=string&file=string&FileName=string

    How do I get my binary file data into that form? I've tried just converting the file data to a string and putting it in the body (file=<string_data>) but I get HTTP 500 errors back, complaining about cannot convert <string_data> to System.Byte. I've got HTTP POST working fine elsewhere in my application with plain string parameters.

    Also, out of curiosity, why is it showing the file parameter twice?


    Friday, April 11, 2014 5:59 AM

All replies

  • I assume you're using managed C++.

    1) To convert a string in bytes: Encoding.GetBytes Method

    2) I don't know why it would show the file parameter twice.  I assume it would work just fine with just a single instance of the parameter.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, April 11, 2014 12:59 PM
    Moderator
  • Hi Matt,

    I am using C++/CX to develop winrt client side.

    How can i convert bytes to string,and using Http Post to Upload the data.

    Thanks a lot,

    waterrock

    Monday, April 14, 2014 3:14 AM