我创建了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();
}
}