建行B2B支付采用Java开发,页面回调采用POST提交方式,编码为GBK。而我们的系统为ASP.NET,编码UTF-8。通过Request获取的参数是乱码,无奈之下,只能从InputStream解析。解析代码如下:
log.Debug("Request Absolute Uri:" + this.Request.Url.AbsoluteUri);
//GBK解析
BinaryReader reader = new BinaryReader(this.Request.InputStream);
Byte[] bs = reader.ReadBytes((int)this.Request.InputStream.Length);
String parameter = HttpUtility.UrlDecode(Encoding.GetEncoding("ISO-8859-1").GetString(bs), Encoding.GetEncoding("GBK"));
NameValueCollection parameterDic = HttpUtility.ParseQueryString(parameter);
if (parameterDic != null && parameterDic.Count > 0)
{
foreach (String key in parameterDic.Keys)
{
log.Debug("Query Param Key:" + key + " Value:" + parameterDic[key]);
}
}
服务端通知采用的GET方式,更郁闷的时,建行竟然对中文参数不做UrlEncode操作,直接用GBK编码回调过来,害得我们浪费了很长时间,还是从最原始的字节码出发,代码如下:
log.Debug("Request Absolute Uri:" + this.Request.Url.AbsoluteUri);
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
byte[] bs = worker.GetQueryStringRawBytes();
String queryString = Encoding.GetEncoding("GBK").GetString(bs);
log.Debug("Query String:" + queryString);
NameValueCollection querys = HttpUtility.ParseQueryString(queryString);
if (querys != null && querys.Count > 0)
{
foreach (String key in querys.AllKeys)
{
log.Debug("Query Param Key:" + key + " Value:" + querys[key]);
}
}