在使用传入的请求错误的编码ASMX Web服务 [英] ASMX Web Service using wrong encoding on incoming request

查看:184
本文介绍了在使用传入的请求错误的编码ASMX Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从客户端

我的.NET Web服务的ASMX是接受请求我没有直接控制权。它发送看起来像这样的请求:

  POST /Service.asmx HTTP / 1.1
连接:保持活动
编译:无缓存
内容长度:1382
内容类型:文本/ XML
接受:文/ XML
主持人:本地主机
用户代理:Borland的SOAP 1.1
SOAPAction报:http://domain.com/InsertRecords< XML版本=1.0&GT?;
< SOAP-ENV:信封...< V>回母校与LT / V>< / SOAP-ENV:信封>

在我的WebMethod,串ALMA的背部被改写的,以?? ?? LM B 13 CK - 典型的编码烂摊子了。

在我的测试中,我发现,如果我只是调整了Content-Type头,一切都很好:

 内容类型:文本/ xml的;字符集= UTF-8

为什么选择.NET比其他的编码UTF-8时,它的不确定,以及有什么办法,我可以强迫这ASMX使用UTF-8编码?


解决方案

Web服务处理程序之前下code运行时调用导致HTTP请求正确去codeD:

 如果(HttpContext.Current.Request.ContentType ==文/ XML){
    HttpContext.Current.Request.ContentType =文本/ XML的,字符集= UTF-8;
}

这感觉有点哈克,但我相信它会为我的情况下工作。我仍然在为什么这是一个问题,在所有的背景资料非常感兴趣,如果有一个更好的方式来退出这个功能(除了让客户更明确一些编码)。

My .NET ASMX webservice is accepting requests from a client I don't have direct control over. It's sending a request that looks like this:

POST /Service.asmx HTTP/1.1
Connection: Keep-Alive
Pragma: no-cache
Content-Length: 1382
Content-Type: text/xml
Accept: text/xml
Host: localhost
User-Agent: Borland SOAP 1.1
SOAPAction: "http://domain.com/InsertRecords"

<?xml version="1.0"?>
<SOAP-ENV:Envelope... <v>ÄLMÅ BÄCK</v></SOAP-ENV:Envelope>

In my WebMethod, the string ÄLMÅ BÄCK gets munged to ??LM?? B??CK -- typical encoding mess-up.

In my testing I've found that if I simply tweak the content-type header, all is well:

Content-Type: text/xml; charset=utf-8

Why is .NET choosing an encoding other than utf-8 when it's unspecified, and is there any way I can coerce this ASMX to use UTF-8 encoding?

解决方案

The following code run before the web service handler is invoked resulted in the HTTP Request correctly decoded:

if (HttpContext.Current.Request.ContentType == "text/xml") {
    HttpContext.Current.Request.ContentType = "text/xml; charset=UTF-8";
}

This feels a bit hacky, but I believe it'll work well for my circumstances. I'm still very interested in some background information on why this was an issue at all, and if there's a better way to pull this off (besides getting the client to be more explicit about the encoding).

这篇关于在使用传入的请求错误的编码ASMX Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆