C# 调用微信红包接口类中第三个参数strEncoding是什么意思,如何取得。请大神指点。
public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
{
try
{
Encoding encoding = Encoding.UTF8;
string cert = @"C:\zhengshu\apiclient_cert.p12";
string password = "xxxxxx";//这里是填写的是 微信支付商户号
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert, password);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.ClientCertificates.Add(cer);
request.Method = "post";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
byte[] buffer = encoding.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
{
return reader.ReadToEnd();
}
}
catch (Exception e)
{
return "";
}
}
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(**strEncoding**)))
就是用上面编码来读取返回的2进制数据流,如utf-8,gb2312。微信一般都是返回utf-8的,直接传递utf-8就行了。如果你请求的是其他url地址,返回的是gb2312编码的你就需要传递gb2312编码
strEncoding就是编码的名称,怎么确定得看URL这个地址返回什么编码的数据,要不编码不对会乱码
https://msdn.microsoft.com/zh-cn/library/t9a3kf7c.aspx
2楼大神的意思是:第三个参数 strEncoding我直接带入“utf-8” 就可以了是吗?