【问题标题】:WCF HTTP POST gives HTTP Status code 400 : Bad RequestWCF HTTP POST 给出 HTTP 状态代码 400:错误请求
【发布时间】:2013-10-25 11:52:26
【问题描述】:

我正在创建一个 WCF 服务应用程序 (REST),它使用 HTTP POST 发送一个只包含字符串的 json,我在使用名为 PostMan 的程序发送 json 来测试时收到 HTTP 状态代码 400 错误请求该服务,源代码如下:-

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfImageUpload
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/JsonData",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json, Method = "POST")]
        bool SendData(JsonString JsonImage);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class JsonString
    {
        [DataMember]
        public string ImageData { get; set; } 
    }
}

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfImageUpload
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public bool SendData(JsonString JsonImage)
        {
            return true;
        }
    }
}

Web.Config

<configuration>
    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
        <services>
            <service behaviorConfiguration="Default" name="WcfImageUpload.Service1">
                <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WcfImageUpload.IService1"/>
                <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="Default">
                    <serviceMetadata httpGetEnabled="true"/>
                </behavior>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
    <system.web>
        <compilation debug="true"/></system.web></configuration>

请提供一些关于正在发生的事情的见解。

【问题讨论】:

    标签: .net json wcf


    【解决方案1】:

    错误的请求错误意味着您在请求中发送的数据格式不正确。

    确保将请求的内容类型设置为 application/json

    此外,如下所示将 WebMessageBodyStyle 设置为 WrappedRequest,以使 WCF 服务期望封装的 JSON 字符串。默认情况下,它需要一个纯字符串。

    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    

    【讨论】:

    • 您的回答指出我应该添加“BodyStyle = WebMessageBodyStyle.WrappedRequest”,但这并不能解决问题,所以我认为其他问题应该是问题,可能是绑定问题。如果您有任何线索,请指出解决方案。
    • 您是否将请求的内容类型设置为“application/json”。我在 ajax 请求中设置它.. 虽然我不确定如何在邮递员中设置它
    • 同样的问题仍然存在,当我发送带有纯文本的 json 时不会发生,但如果我发送带有值的 json,就会出现问题。我得到了 json 作为回报,有什么线索吗?
    • 您能否仅发布您发送的示例数据。因为我还是怀疑数据格式
    • 我可以发送 JSON,因为现在我使用的是流而不是自定义类,具有相同的 WebInvoke 参数,但是在将图像转换为 Byte64 格式后我无法发送图像,我猜这可能因为字符串很长,现在在客户端尝试使用 MultipartEntity。您要求的样本数据是:- { "ImageData" : "abcdefgh" }
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 2017-09-07
    • 2018-08-09
    • 2019-05-28
    • 2014-11-26
    • 1970-01-01
    • 2015-10-15
    • 2021-09-24
    相关资源
    最近更新 更多