响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。

时间:2021-12-16 09:15:56
环境描述
服务器 2003、sql server2008、.NET Framework 3.5、IIS6.0

问题描述:
1、在客户端用VS2008的ASP.NET Development Server进行Debug正常(XP、WIN7都正常);
2、将WebService生成网站部署到以上的服务器上面,
  XP下面的客户端可以正常使用,WIN7下面提示异常。
  WIN7下面的异常信息如下:
    
远程服务器返回错误: (500) 内部服务器错误。
  响应消息的内容类型 text/html; charset=utf-8 与绑定(text/xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节为:“<html>\r\n <head>\r\n <title>运行时错误</title>\r\n <style>\r\n body {font-family:\"Verdana\";font-weight:normal;font-size: .7em;color:black;} \r\n p {font-family:\"Verdana\";font-weight:normal;color:black;margin-top: -5px}\r\n b {font-family:\"Verdana\";font-weight:bold;color:black;margin-top: -5px}\r\n H1 { font-family:\"Verdana\";font-weight:normal;font-size:18pt;color:red }\r\n H2 { font-family:\"Verdana\";font-weight:normal;font-size:14pt;color:maroon }\r\n pre {font-family:\"Lucida Console\";font-size: .9em}\r\n .marker {font-weight: bold; color: black;text-decoration: none;}\r\n .version {color: gray;}\r\n .error {margin-bottom: 10px;}\r\n .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }\r\n </style>\r\n </head>\r\n\r\n <body bgcolor=\"white\">\r\n\r\n <span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>\r\n\r\n <h2> <i>运行时错误</i> </h2><”。

后来去查看了一下服务器的IIS日志:
2012-06-01 04:33:58 W3SVC269270746 192.168.1.235 GET /dkCRMWebService.asmx - 8000 - 192.168.1.235 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2;+SV1;+.NET+CLR+1.1.4322;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.4506.2152;+.NET+CLR+3.5.30729) 200 0 0
2012-06-01 04:34:40 W3SVC269270746 192.168.1.235 POST /dkCRMWebService.asmx - 8000 - 192.168.1.101 - 500 0 0

这样看来应该是程序在往客户端POST的时候出错了。

另外:服务器上IIS没有问题,因为我部署了另外一个跟这个程序差不多的程序,能够正常访问。

希望有类似经验的帮我想个办法,着急~~~~~

16 个解决方案

#1


看这个页面代码dkCRMWebService.asmx

#2


以下是dkCRMWebService.asmx的代码

<%@ WebService Language="C#" CodeBehind="~/App_Code/dkCRMWebService.cs" Class="dkCRMWebService" %>


dkCRMWebService.cs的代码有点长,不知道您需要哪段?

#3


代码错误,你应该自己学会调试。
比如
先直接返回Hello看能否正确,
依次添加更多的代码。或者加入断点

#4


遇到问题要学会排除,将可能出现错误的范围尽量缩小。然后如果还是不能解决,那就贴出核心代码

#5


响应消息的内容类型 text/ html; charset=utf-8 与绑定(text/ xml; charset=utf-8)的内容类型不匹配

这是什么情况捏?

#6


问题大体上知道出在什么地方了,我在Webservice上加了SOAP头认证,在web.config的<httpModules>节点下追加了一个Item:
<add name="WebServiceAuthenticationModule" type="dongke.dkCRM.WebService.Manager.WebServiceAuthenticationModule" />
也就是说在所有的webservice方法调用之前要走WebServiceAuthenticationModule认证。

App_Code\WebServiceAuthenticationModule.cs大体上的代码如下:

public sealed class WebServiceAuthenticationModule : IHttpModule
{
public const string CACHEKEY_FORMAT = "AUTHINFO_{0}";

/// <summary>
/// 类初始化,新建一个认证事件
/// </summary>
/// <param name="httpApp"></param>
public void Init(HttpApplication httpApp)
{
httpApp.AuthenticateRequest += new EventHandler(httpApp_AuthenticateRequest);
}

/// <summary>
/// 类释放
/// </summary>
public void Dispose()
{
}

/// <summary>
/// 对调用Webservice进行合法性验证
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void httpApp_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
Stream httpStream = context.Request.InputStream;

// Save the current position of stream.
long posStream = httpStream.Position;

// If the request contains an HTTP_SOAPACTION 
// header, look at this message.
if (context.Request.ServerVariables["HTTP_SOAPACTION"] == null)
{
return;
}

// Load the body of the HTTP message
// into an XML document.
XmlDocument dom = new XmlDocument();
AuthenticationSoapHeader authInfo = null;

try
{
dom.Load(httpStream);

// Reset the stream position.
httpStream.Position = posStream;

XmlNodeList nodeValidationKey = dom.GetElementsByTagName("ValidationKey");
XmlNodeList nodeValidationDT = dom.GetElementsByTagName("ValidationDateTime");
XmlNodeList nodeValidationCode = dom.GetElementsByTagName("ValidationCode");

string validationKey = null;
DateTime validationDT = DateTime.MaxValue;
string validationCode = null;

bool isCheck = false;
if (nodeValidationKey != null && nodeValidationKey.Count != 0
&& nodeValidationDT != null && nodeValidationDT.Count != 0
&& nodeValidationCode != null && nodeValidationCode.Count != 0)
{
validationKey = nodeValidationKey.Item(0).InnerText;

if (!DateTime.TryParse(nodeValidationDT.Item(0).InnerText, out validationDT))
{
isCheck = true;
}
validationCode = nodeValidationCode.Item(0).InnerText;
}

if (isCheck)
{
throw new SoapException("本Webservice只允许客户端进行调用,不允许远程直接调用。",
  new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

string key = Crypt.DecryptDES(validationKey, validationDT.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
string code = null;
if (validationDT.Second < 30)
{
code = Crypt.GetMD5String(key);
}
else if (validationDT.Second < 40)
{
code = Crypt.GetSHA1String(key);
}
else
{
code = Crypt.GetSHA256String(key);
}

if (!validationCode.Equals(code))
{
throw new SoapException("本Webservice只允许客户端进行调用,不允许远程直接调用。",
   new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

XmlNodeList nodeAuthenticateLogin = dom.GetElementsByTagName("Authenticate");
if ((nodeAuthenticateLogin != null && 0 < nodeAuthenticateLogin.Count))
{
return;
}

// 认证的信息不正确,抛出一个异常
XmlNodeList nodeStaffID = dom.GetElementsByTagName("StaffID");
XmlNodeList nodeUseStaffPassword = dom.GetElementsByTagName("StaffPassword");
XmlNodeList nodeSessionKey = dom.GetElementsByTagName("SessionKey");

if (nodeStaffID == null
|| nodeStaffID.Count == 0
|| nodeUseStaffPassword == null
|| nodeUseStaffPassword.Count == 0
|| nodeSessionKey == null
|| nodeSessionKey.Count == 0)
{
throw new SoapException("WebService访问的权限不够,请联系系统管理员。",
new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

// 给认证的头信息重新赋值
authInfo = new AuthenticationSoapHeader();
authInfo.StaffID= Convert.ToInt32(nodeStaffID.Item(0).InnerText);
authInfo.StaffPassword = nodeUseStaffPassword.Item(0).InnerText;
authInfo.SessionKey = nodeSessionKey.Item(0).InnerText;
}
catch (Exception ex)
{
httpStream.Position = posStream;

// Throw a SOAP exception.
XmlQualifiedName name = new XmlQualifiedName("httpApp_AuthenticateRequest");
SoapException soapException =
new SoapException("Unable to read SOAP request", name, ex);
throw soapException;
}
// 执行认证处理
Authenticate(authInfo, context);
}

/// <summary>
/// webservice的SOAP头认证处理
/// </summary>
/// <param name="auth">头认证的用户信息</param>
/// <param name="context">请求内容</param>
private void Authenticate(AuthenticationSoapHeader auth, HttpContext context)
{
int staffID = -1;
if (!dongke.dkCRM.WebService.Function.dkCRMFunction.AuthenticateSoap(auth,ref staffID))
{
// 认证失败的情况
throw new SoapException("用户认证失败", new XmlQualifiedName("Authenticate"));
}
else
{
// 认证成功的情况
context.Items[dkCRMWebService.CONTEXTITEMS_STAFFID] = staffID;
}
}

应该是在这部分代码出错了,但是奇怪的是Debug的时候,不会有问题,
而且在XP和2003操作系统下面用编译完的客户端访问部署在2003上的Webservice不会出错,
WIN7下访问就会出错。

不知道大家做WebService的SOAP头认证都用什么方法。

#7


问题解决,心情好,散分~~

#8


请问最后怎么解决了?

#9


亲,解决方法是什么,贴出来咧

#10


对呀,我也想知道解决方法是什么?是不是取消对WebServiceAuthenticationModule认证?

#11


像这样时间有很大差距的帖子,有人提问题版主不能及时答复,衷心祝愿CSDN贵机构把软件程序做的更人性化一些,提醒发帖人有新消息。

#12


解决了问题说一下!!!楼主贱人

#13


louzhu  wo jiushi yao kuangcaonimaboyibi

#14


你用SOAP调用? SOAP是基于xml的,怎么传json

#15


楼主贱人,解决也不贴下,祝你一辈子屌丝

#16


引用 15 楼 wei0654 的回复:
楼主贱人,解决也不贴下,祝你一辈子屌丝


狂顶,最鄙视这种自私的人!

#1


看这个页面代码dkCRMWebService.asmx

#2


以下是dkCRMWebService.asmx的代码

<%@ WebService Language="C#" CodeBehind="~/App_Code/dkCRMWebService.cs" Class="dkCRMWebService" %>


dkCRMWebService.cs的代码有点长,不知道您需要哪段?

#3


代码错误,你应该自己学会调试。
比如
先直接返回Hello看能否正确,
依次添加更多的代码。或者加入断点

#4


遇到问题要学会排除,将可能出现错误的范围尽量缩小。然后如果还是不能解决,那就贴出核心代码

#5


响应消息的内容类型 text/ html; charset=utf-8 与绑定(text/ xml; charset=utf-8)的内容类型不匹配

这是什么情况捏?

#6


问题大体上知道出在什么地方了,我在Webservice上加了SOAP头认证,在web.config的<httpModules>节点下追加了一个Item:
<add name="WebServiceAuthenticationModule" type="dongke.dkCRM.WebService.Manager.WebServiceAuthenticationModule" />
也就是说在所有的webservice方法调用之前要走WebServiceAuthenticationModule认证。

App_Code\WebServiceAuthenticationModule.cs大体上的代码如下:

public sealed class WebServiceAuthenticationModule : IHttpModule
{
public const string CACHEKEY_FORMAT = "AUTHINFO_{0}";

/// <summary>
/// 类初始化,新建一个认证事件
/// </summary>
/// <param name="httpApp"></param>
public void Init(HttpApplication httpApp)
{
httpApp.AuthenticateRequest += new EventHandler(httpApp_AuthenticateRequest);
}

/// <summary>
/// 类释放
/// </summary>
public void Dispose()
{
}

/// <summary>
/// 对调用Webservice进行合法性验证
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void httpApp_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
Stream httpStream = context.Request.InputStream;

// Save the current position of stream.
long posStream = httpStream.Position;

// If the request contains an HTTP_SOAPACTION 
// header, look at this message.
if (context.Request.ServerVariables["HTTP_SOAPACTION"] == null)
{
return;
}

// Load the body of the HTTP message
// into an XML document.
XmlDocument dom = new XmlDocument();
AuthenticationSoapHeader authInfo = null;

try
{
dom.Load(httpStream);

// Reset the stream position.
httpStream.Position = posStream;

XmlNodeList nodeValidationKey = dom.GetElementsByTagName("ValidationKey");
XmlNodeList nodeValidationDT = dom.GetElementsByTagName("ValidationDateTime");
XmlNodeList nodeValidationCode = dom.GetElementsByTagName("ValidationCode");

string validationKey = null;
DateTime validationDT = DateTime.MaxValue;
string validationCode = null;

bool isCheck = false;
if (nodeValidationKey != null && nodeValidationKey.Count != 0
&& nodeValidationDT != null && nodeValidationDT.Count != 0
&& nodeValidationCode != null && nodeValidationCode.Count != 0)
{
validationKey = nodeValidationKey.Item(0).InnerText;

if (!DateTime.TryParse(nodeValidationDT.Item(0).InnerText, out validationDT))
{
isCheck = true;
}
validationCode = nodeValidationCode.Item(0).InnerText;
}

if (isCheck)
{
throw new SoapException("本Webservice只允许客户端进行调用,不允许远程直接调用。",
  new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

string key = Crypt.DecryptDES(validationKey, validationDT.ToString("yyyy/MM/dd HH:mm:ss.fffffff"));
string code = null;
if (validationDT.Second < 30)
{
code = Crypt.GetMD5String(key);
}
else if (validationDT.Second < 40)
{
code = Crypt.GetSHA1String(key);
}
else
{
code = Crypt.GetSHA256String(key);
}

if (!validationCode.Equals(code))
{
throw new SoapException("本Webservice只允许客户端进行调用,不允许远程直接调用。",
   new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

XmlNodeList nodeAuthenticateLogin = dom.GetElementsByTagName("Authenticate");
if ((nodeAuthenticateLogin != null && 0 < nodeAuthenticateLogin.Count))
{
return;
}

// 认证的信息不正确,抛出一个异常
XmlNodeList nodeStaffID = dom.GetElementsByTagName("StaffID");
XmlNodeList nodeUseStaffPassword = dom.GetElementsByTagName("StaffPassword");
XmlNodeList nodeSessionKey = dom.GetElementsByTagName("SessionKey");

if (nodeStaffID == null
|| nodeStaffID.Count == 0
|| nodeUseStaffPassword == null
|| nodeUseStaffPassword.Count == 0
|| nodeSessionKey == null
|| nodeSessionKey.Count == 0)
{
throw new SoapException("WebService访问的权限不够,请联系系统管理员。",
new XmlQualifiedName("httpApp_AuthenticateRequest"));
}

// 给认证的头信息重新赋值
authInfo = new AuthenticationSoapHeader();
authInfo.StaffID= Convert.ToInt32(nodeStaffID.Item(0).InnerText);
authInfo.StaffPassword = nodeUseStaffPassword.Item(0).InnerText;
authInfo.SessionKey = nodeSessionKey.Item(0).InnerText;
}
catch (Exception ex)
{
httpStream.Position = posStream;

// Throw a SOAP exception.
XmlQualifiedName name = new XmlQualifiedName("httpApp_AuthenticateRequest");
SoapException soapException =
new SoapException("Unable to read SOAP request", name, ex);
throw soapException;
}
// 执行认证处理
Authenticate(authInfo, context);
}

/// <summary>
/// webservice的SOAP头认证处理
/// </summary>
/// <param name="auth">头认证的用户信息</param>
/// <param name="context">请求内容</param>
private void Authenticate(AuthenticationSoapHeader auth, HttpContext context)
{
int staffID = -1;
if (!dongke.dkCRM.WebService.Function.dkCRMFunction.AuthenticateSoap(auth,ref staffID))
{
// 认证失败的情况
throw new SoapException("用户认证失败", new XmlQualifiedName("Authenticate"));
}
else
{
// 认证成功的情况
context.Items[dkCRMWebService.CONTEXTITEMS_STAFFID] = staffID;
}
}

应该是在这部分代码出错了,但是奇怪的是Debug的时候,不会有问题,
而且在XP和2003操作系统下面用编译完的客户端访问部署在2003上的Webservice不会出错,
WIN7下访问就会出错。

不知道大家做WebService的SOAP头认证都用什么方法。

#7


问题解决,心情好,散分~~

#8


请问最后怎么解决了?

#9


亲,解决方法是什么,贴出来咧

#10


对呀,我也想知道解决方法是什么?是不是取消对WebServiceAuthenticationModule认证?

#11


像这样时间有很大差距的帖子,有人提问题版主不能及时答复,衷心祝愿CSDN贵机构把软件程序做的更人性化一些,提醒发帖人有新消息。

#12


解决了问题说一下!!!楼主贱人

#13


louzhu  wo jiushi yao kuangcaonimaboyibi

#14


你用SOAP调用? SOAP是基于xml的,怎么传json

#15


楼主贱人,解决也不贴下,祝你一辈子屌丝

#16


引用 15 楼 wei0654 的回复:
楼主贱人,解决也不贴下,祝你一辈子屌丝


狂顶,最鄙视这种自私的人!