【问题标题】:How to return response as Content-Type: application/x-www-form-urlencoded; from WCF service?如何以 Content-Type 的形式返回响应:application/x-www-form-urlencoded;来自 WCF 服务?
【发布时间】:2014-09-23 08:29:01
【问题描述】:

花费大量时间最终让我的 WCF 服务以Content-Type: application/x-www-form-urlencoded; 的身份接受请求(使用此处建议的 Stream Best way to support "application/x-www-form-urlencoded" post data with WCF?

终于成功了。

但是我得到的响应是

Content-Type: application/xml; charset=utf-8
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">result</string>

但我想要的是

Content-Type: application/x-www-form-urlencoded; charset=UTF-8
result

可以在哪里设置/破解?

目前方法如下:

[WebInvoke(UriTemplate = "generate_license", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
[OperationContract]
string GenerateLicense(Stream period);

【问题讨论】:

  • 我认为响应类型为 application/x-www-form-urlencoded 没有意义,您是否向客户发布了表单?您想通过将响应设置为内容类型来实现什么目标?
  • 好吧,也许这没有意义。我正在做的是接受来自过时网络服务的一些简单调用并生成许可证文本并将其发送给他们,他们也将其作为原始文本除外..不是 XML
  • 如果你想发送纯文本,那么text/plain 更有意义。

标签: c# wcf


【解决方案1】:

好的,我好像解决了:

public Stream GenerateLicense(Stream period)
        {
            Stream result = new MemoryStream();

            StreamWriter sw = new StreamWriter(result);
            sw.Write("LICENSE");
            sw.Flush();
            result.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            return result;
        }

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2013-11-10
    • 2019-10-26
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2019-05-24
    • 2018-12-27
    • 2018-05-12
    相关资源
    最近更新 更多