【问题标题】:POST request in REST WCFREST WCF 中的 POST 请求
【发布时间】:2014-04-04 17:11:06
【问题描述】:

我开发了一个REST WCF服务方法如下:

 [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Details")]
    DetailData GetDetails(TestData requst);


 [DataContract]
public class TestData
{
    [DataMember]
    public string DetailData { get; set; }
}

现在我正在尝试使用以下客户端代码调用服务:

  ASCIIEncoding encoding = new ASCIIEncoding();
  string testXml = "<TestData>" +
                      "<DetailData>" +
                          "4000" +
                       "</DetailData>" +
                    "</TestData>";

 string postData = testXml.ToString();
 byte[] data = encoding.GetBytes(postData);
 string url = "http://localhost/WCFRestService.svc/bh/Details";
 string strResult = string.Empty;

 // declare httpwebrequet wrt url defined above
 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
 // set method as post
 webrequest.Method = "POST";
 // set content type
 webrequest.ContentType = "text/xml";
 // set content length
 webrequest.ContentLength = data.Length;
 // get stream data out of webrequest object
 Stream newStream = webrequest.GetRequestStream();
 newStream.Write(data, 0, data.Length);
 newStream.Close();

 //Gets the response
 WebResponse response = webrequest.GetResponse();
 //Writes the Response
 Stream responseStream = response.GetResponseStream();

 StreamReader sr = new StreamReader(responseStream);
 string s = sr.ReadToEnd();

我收到以下错误:

“远程服务器返回错误:(400) Bad Request”

我可以成功调用另一个使用“GET”动词的服务方法。但是上面使用“POST”动词调用服务的客户端代码不起作用。我想,我在客户端代码中遗漏了一些东西。 可能是什么问题?

【问题讨论】:

    标签: wcf rest


    【解决方案1】:

    尝试改变

    WebMessageBodyStyle.WrappedRequest
    

    WebMessageBodyStyle.Bare

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2018-07-14
      • 2018-08-04
      • 2021-05-30
      相关资源
      最近更新 更多