WCF错误请求400 - wpf客户端

时间:2016-04-26 19:14:00

标签: c# wpf wcf rest

我创建了WCF服务和WPF客户端来发送服务某些流。 GET方法在我的客户端工作正常,但我有POST方法的问题。这是代码: IRestService:

[OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat =     WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "SaveFromStreamJson2")]
    void SaveFromStreamJson2(Stream stream);

RestService.svc

public void SaveFromStreamJson2(Stream stream)
    {
        if (stream != null)
        {
            StreamReader stReader = new StreamReader(stream);
            string text = stReader.ReadToEnd();
        }

    }

WCF客户端:

private void button2_Click(object sender, RoutedEventArgs e)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:57424/RestService.svc/SaveFromStreamJson2");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"user\":\"test\"," + "\"password\":\"bla\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }

0 个答案:

没有答案