【问题标题】:Problem calling a simple C# Webservice method from Postman从 Postman 调用简单的 C# Webservice 方法时出现问题
【发布时间】:2021-03-22 20:02:10
【问题描述】:

我使用 C# 创建了一个非常简单的 Web 服务

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Receival
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    
    public class ReceiveService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string Mirror(string request)
        {
            return request;
        }
    }
}

我正在使用 Postman 进行测试。

HelloWorld() 工作正常

Mirror() 返回错误:

System.InvalidOperationException:请求格式无效: 文本/纯文本。在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

无论我在 PostMan 请求标头中使用的 ContentType 是什么

任何帮助表示赞赏。谢谢

【问题讨论】:

  • 默认的内容类型是text/xml。你试过用它吗?
  • 如果您需要将其更改为例如application/json,那么您可以使用[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json]
  • 谢谢彼得 Csala。但是我应该把它放在我的代码哪里呢?
  • 我使用 asmx :(

标签: c# web-services postman


【解决方案1】:

'Request' 是一个 .Net 对象 - 在返回之前转换为字符串类型,如 text/json/html。

【讨论】:

  • 我无法确定在 Web 服务的哪个事件中,请求会弹出,因此我可以对其进行操作:(
  • 尝试替换'return request;'以“return request.ToString()”为例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多