Wcf post方法,返回400错误请求 [英] Wcf post method, returns 400 bad request

查看:89
本文介绍了Wcf post方法,返回400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到同一个问题有很多答案,但我无法解决我的问题,你们中的任何一个人都可以通过我的代码解决我的问题。我已经创建了一个WCF服务,如下所示POST方法



接口(iComplaints.cs)

I see there are many answers to the same question but I am unable to resolve mine, can any one of you please go through my code and resolve my issue please. I have created a WCF Service as below The POST Method

Interface (iComplaints.cs)

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,     ResponseFormat = WebMessageFormat.Json, UriTemplate = "/insertcomplaint")]
Stream InsertComplaint(ComplaintData data);



Class(Complaints.cs)


Class (Complaints.cs)

public Stream InsertComplaint(ComplaintData data)
{
    //the code
}



DataContract类(ComplaintData)


DataContract class (ComplaintData)

public class ComplaintData
{
   [DataMember]
    public string ComplaintID { get; set; }
    [DataMember]
    public string EntryBy { get; set; }
}



我在本地托管了这项服务,当我尝试使用下面的客户端方法使用它时,它给了我400(错误请求)


I have hosted the service locally and when i try to consume it using the below client method it's giving me 400 (Bad Request)

void PostComplaint()
    {
        HttpWebRequest req = null;
        HttpWebResponse res = null;
            string url = "http://localhost:28522/Complaints.svc/insertcomplaint";

            ComplaintData iData = new ComplaintData();
            iData.ComplaintID = txtComplaintID.Text;
            iData.EntryBy = txtEntryBy.Text;

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json"; 
            req.Headers.Add("SOAPAction", url);

            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(iData);
            }

            res = (HttpWebResponse)req.GetResponse();
            using (var streamReader = new StreamReader(res.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                TextBox1.Text = result;
            }
    }



WCF服务的Web配置


Web Config of the WCF Service

<service behaviorConfiguration="ServiceBehavior" name="Complaints">
    <endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="IComplaints" />
   <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>



请有人帮忙。



我有什么尝试过:



如果没有成功,我尝试更改BodyStyle


Please anyone help.

What I have tried:

With no success I tried changing the BodyStyle

WebMessageBodyStyle.WrappedRequest



我也尝试将json请求作为字符串发送但是导致post方法中的输入参数为null。


I also tried sending the json request as a string but that resulted in input paramater as null in the post method.

推荐答案

我自己解决了这个问题

在我的客户端我更改了

I solved this myself
In my client i changed from
void PostComplaint()
    {
        HttpWebRequest req = null;
        HttpWebResponse res = null;
            string url = "http://localhost:28522/Complaints.svc/insertcomplaint";
 
            ComplaintData iData = new ComplaintData();
            iData.ComplaintID = txtComplaintID.Text;
            iData.EntryBy = txtEntryBy.Text;
 
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json"; 
            req.Headers.Add("SOAPAction", url);
 
            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(iData);
            }
 
            res = (HttpWebResponse)req.GetResponse();
            using (var streamReader = new StreamReader(res.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                TextBox1.Text = result;
            }
    }





to



to

void PostComplaint()
    {
    string url = "http://localhost:28522/Complaints.svc/insertcomplaint";
               Uri uri = new Uri(url);

               req = (HttpWebRequest)WebRequest.Create(uri);
               req.Method = "POST";
               req.ContentType = "application/json";
               req.Headers.Add("SOAPAction", url);

//Commented
 //ComplaintData iData = new ComplaintData();
            //iData.ComplaintID = txtComplaintID.Text;
            //iData.EntryBy = txtEntryBy.Text;

//Added
               var iData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { ComplaintID = txtComplaintID.Text, EntryBy = txtEntryBy.Text });
//
               using (var streamWriter = new StreamWriter(req.GetRequestStream()))
               {
                   streamWriter.Write(iData);
               }

               res = (HttpWebResponse)req.GetResponse();
               using (var streamReader = new StreamReader(res.GetResponseStream()))
               {
                   var result = streamReader.ReadToEnd();
                   TextBox1.Text = result;
               }
}


这篇关于Wcf post方法,返回400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆