Getting a POST endpoint to work in self-hosted (WebServiceHost) C# webservice?

前端 未结 2 404
时光取名叫无心
时光取名叫无心 2020-12-01 23:16

So, I have been messing around with webservices for a while now, and I keep getting back to some basics, that I never seem to get right.

Question 1:

When u

2条回答
  •  误落风尘
    2020-12-01 23:52

    I found out the answer. This is how to define a method, that can take the raw data sent in a HTTP POST:

    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare)]
    Stream PutMessage(Stream data);
    

    and the implementation is like this:

    public Stream PutMessage(Stream data)
    {
        byte[] buffer = new byte[65535];
    
        int bytesRead, totalBytes = 0;
        do
        {
            bytesRead = data.Read(buffer, 0, 65535);
            totalBytes += bytesRead;
        }
        while (bytesRead > 0);
    
        // Then you could interpret it as a String for example:
        string jsonString = Encoding.UTF8.GetString(buffer, 0, totalBytes);
        // yada yada
    }
    

提交回复
热议问题