简体   繁体   English

400错误的请求-REST JSON c#

[英]400 Bad request - REST JSON c#

I've created a C# WCF project, when I'm testing it using POSTMAN or Fiddler. 使用POSTMAN或Fiddler进行测试时,我已经创建了一个C#WCF项目。 I've received an error: "400 Bad Request". 我收到一个错误:“ 400错误的请求”。 Also I've created an C# windows project to test, it is the same result as well. 我还创建了一个C#Windows项目进行测试,结果也相同。 Here's my WCF Project. 这是我的WCF项目。

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "api/createpr.json")]
    PRApplication AddPR(RequestData rData);

}

I'm using this code when testing. 我在测试时使用此代码。

            try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/WEB_Service_B1/Service1.svc/api/createpr.json");
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"Test\"}";

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

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }

Is there any problem also in my Web.config? 我的Web.config中也有问题吗?

 <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Windows" /> <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> </system.web> <system.webServer> <directoryBrowse enabled="true"/> </system.webServer> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="WS_B1.Service1Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <services> <service name="WS_B1.Service1" behaviorConfiguration="WS_B1.Service1Behavior"> <endpoint address="" binding="webHttpBinding" contract="WS_B1.IService1" behaviorConfiguration="web"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> </system.serviceModel> </configuration> 

BodyStyle = WebMessageBodyStyle.Wrapped

This means the request needs to be a full json object. 这意味着请求必须是完整的json对象。 Try with the following request body: 尝试使用以下请求正文:

string json = "{\"rData\": {\"PropertyName\": \"Test\"}}";

Where PropertyName is a public property of RequestData class of string type. 其中PropertyName是字符串类型的RequestData类的公共属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM