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

前端 未结 2 385
时光取名叫无心
时光取名叫无心 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:46

    I think a simple code can answer all your questions

    Task.Factory.StartNew(()=>StartServer());
    Thread.Yield();
    StartClient();
    

    void StartServer()
    {
        Uri uri = new Uri("http://localhost:8080/test");
        WebServiceHost host = new WebServiceHost(typeof(WCFTestServer), uri);
        host.Open();
    }
    
    void StartClient()
    {
        try
        {
            WebClient wc = new WebClient();
    
            //GET
            string response1 = wc.DownloadString("http://localhost:8080/test/PutMessageGET/abcdef");
            //returns: "fedcba"
    
            //POST with UriTemplate
            string response2 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTUriTemplate/abcdef",
                                                JsonConvert.SerializeObject(new { str = "12345" }));
            //returns: fedcba NOT 54321
    
    
            //POST with BodyStyle=WebMessageBodyStyle.WrappedRequest
            //Request: {"str":"12345"}
            wc.Headers["Content-Type"] = "application/json";
            string response3 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTWrappedRequest",
                                                JsonConvert.SerializeObject(new { str="12345" }));
    
            //POST with BodyStyle=WebMessageBodyStyle.Bare
            wc.Headers["Content-Type"] = "application/json";
            string response4 = wc.UploadString("http://localhost:8080/test/PutMessagePOSTBare", "12345" );
    
        }
        catch (WebException wex)
        {
            Console.WriteLine(wex.Message);
        }
    }
    

    [ServiceContract]
    public class WCFTestServer
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/PutMessageGET/{str}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public string PutMessageGET(string str)
        {
            return String.Join("", str.Reverse());
        }
    
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/PutMessagePOSTUriTemplate/{str}", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public string PutMessagePOSTUriTemplate(string str)
        {
            return String.Join("", str.Reverse());
        }
    
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public string PutMessagePOSTWrappedRequest(string str)
        {
            return String.Join("", str.Reverse());
        }
    
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public string PutMessagePOSTBare(string str)
        {
            return String.Join("", str.Reverse());
        }
    }
    

    PS: You can find the JsonConvert here

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题