c# - 获取 WCF 响应的内容类型

标签 c# wcf http mime

我有一个从服务器下载内容的 WCF 客户端。

服务契约(Contract)是;

[OperationContract]
        [WebGet(
                UriTemplate = "/my/service/url/{method}/{filename}?tradeId={tradeId}&docType={docType}&language={language}&version={version}",
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare)]
        Stream GetDocument(string method, string filename, string tradeId, string docType, string version, string language);

返回类型是 Stream。我所做的只是将该流写入一个文件,它就可以工作了。

现在,我想对此进行修改。我想知道下载文档的 MIME 类型。我知道它在服务器上设置正确。我只需要取回它。

我对 WCF 经验不多,也不知道该怎么做。有人可以告诉我吗?

非常感谢

最佳答案

您必须有权访问 OperationContextWebOperationContext。要在客户端上实现这一点,请使用 OperationContextScope:

using (var scope = new OperationContextScope((IContextChannel)proxy))
{
    Stream document = proxy.GetDocument(...);
    string contentType = WebOperationContext.Current.IncomingResponse.ContentType;
}

关于c# - 获取 WCF 响应的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5350758/

相关文章:

c# - Paypal C# 返回 Url

c# - for循环中N个数的阶乘

c# - 如何理解 WCF 跟踪

java - 查看 HttpURLConnection 中的详细日志

php - 如何使用 file_get_contents 和 expedia XML API 修复 411 Length Required 错误?

python - 在 Python 中编码 HTTP 请求

c# - 使用 SharpDX 绘制 SVG 图形

c# - LINQ to SQL 和自关联表

c# - 如何在 wcf-service 中设置默认的 faultcontract 属性

wcf - 多线程单例WCF服务