当前位置:编程学习 > C#/ASP.NET >>

用C#做一个REST的WCF,用POST方法总是返回400错误

RT

接口协议:
[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/FeedBack/Submit", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        ErrorCode SubClientFeedback(FeedBack userFeedBack);

Client:
string strText;
            
            Uri address = new Uri("http://mobi4biz.net" + "/mobi4bizservice.svc/FeedBack/Submit");
            // Create the web request   
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            // Set type to POST   
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";
            //request.ContentType = "text/x-json"; 
            // Create the data we want to send   
            string data = "{ \"usrID\": 1, \"FeedBackType\": 1, \"ContactName\": \"afd dfad \", \"ContactPhone\": \"123534234\", \"ContactEmail\": \"asf@asdf.com\"}";
            // Create a byte array of the data we want to send   
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
            // Set the content length in the request headers   
            request.ContentLength = byteData.Length;
            // Write data   
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
            // Get response   
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream   
                StreamReader reader = new StreamReader(response.GetResponseStream());
                // Console application output   
                strText = strText + reader.ReadToEnd();
            }
            strText = strText + "\n";
            lblStatus.Text = strText;

我用AJAX调用也是同样的问题。
function HttpPost() {
                 var str = "{ \"usrID\": 1, \"FeedBackType\": 1, \"ContactName\": \"afd dfad \", \"ContactPhone\": \"123534234\", \"ContactEmail\": \"asf@asdf.com\"}";
                 $.ajax({
                     type: "POST",
                     contentType: "application/json",
                     url: "http://localhost:6023/Testservice.svc/FeedBack/Submit",
                     data: str,
                     success: function(data) {
                         $("#TextPost").val(data);
                     }
                 });
             }

不知道是什么问题?会不会是namespace的问题?请高手指点一下!谢谢!!!! --------------------编程问答-------------------- --------------------编程问答-------------------- 自己顶起来! --------------------编程问答-------------------- 你的url 地址实用可能有问题。我也遇到过类似的问题:把URL换成相对路径就好用了。据资料说是跨域问题造成。 --------------------编程问答-------------------- http://blog.csdn.net/fangxinggood/archive/2011/03/14/6247297.aspx

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,