【问题标题】:How to accept JSON in SOAP WS, without XML如何在没有 XML 的 SOAP WS 中接受 JSON
【发布时间】:2016-06-20 21:03:29
【问题描述】:

我在 .Net 3.5 版本中用 SOAP 编写了一个 WEBSERVICE。当你在 IE 上显示 ASMX 文件时,它会显示如下信息。

我告诉我的客户,我可以接受他们发送给我的 JSON 对象。我正在接受一个令牌(字符串)和 param1(JSON 对象)。但是,token 和 param1 仍然在 XML 中发送。客户告诉我他只想给我发送 json。我怎样才能按照他想要的方式改变它?谁能告诉我该怎么做?

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/PaymentRequest"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PaymentRequest xmlns="http://tempuri.org/">
      <guid>string</guid>
      <param1>
        <organisationId>string</organisationId>
        <kdv>decimal</kdv>
        <bsmv>decimal</bsmv>
        <payment>
          <paymentId>string</paymentId>
          <signedDate>dateTime</signedDate>
          <amount>decimal</amount>
          <installment>int</installment>
          <appliedRate>decimal</appliedRate>
          <cardId>string</cardId>
        </payment>
      </param1>
    </PaymentRequest>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

    标签: c# json xml web-services soap


    【解决方案1】:

    1-) 你在[ScriptMethod(ResponseFormat = ResponseFormat.Json)]上方添加你的服务

    像这样

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
             JavaScriptSerializer js = new JavaScriptSerializer();
             Context.Response.Clear();
             Context.Response.ContentType = "application/json";           
             Context.Response.Write(js.Serialize("HelloWorld"));
        }
    

    2-) 添加您的 web.config 这个处理程序部分

    configuration>
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
        </system.web>
        <system.webServer>
            <handlers>
                <add name="ScriptHandlerFactory"
                     verb="*" path="*.asmx"
                     type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                     resourceType="Unspecified" />
            </handlers>
        </system.webServer>
    </configuration>
    

    【讨论】:

    • 但是,我不想用 JSON 响应,我想接收 JSON 参数
    • 您的客户使用此格式发送数据 ContentType = "application/json" ?
    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多