asp.net调身份证识别接口,json数据乱码
n无关风月 发布于2018-04-16 浏览:991 回复:6
0
收藏
调用代码如下:
string strbaser64 = ImgToBase64String(path); // 图片的base64编码
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
String str = "id_card_side=front&image=" + HttpUtility.UrlEncode(strbaser64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("身份证识别:");
Console.WriteLine(result);
return result;
 
返回的result中,汉子全是乱码,数字和字母正常,是哪里出了问题?
收藏
点赞
0
个赞
共6条回复 最后由lichao_86回复于2018-10-05
#7lichao_86回复于2018-10-05

url编码的问题

0
#6追星逐月的家回复于2018-07-10

结果流编码错误了吧

0
#5n无关风月回复于2018-05-14
#3 goJhou回复
streamreader选错编码了

是的改成treamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));就好了

0
#4n无关风月回复于2018-05-14
#2 756665228回复
 HttpUtility.UrlEncode(strbaser64); 你这里需要加UTF-8。 之前一个C#也是遇到这样的问题。说加了UTF-8就好了。具体加哪里我也不清楚。我是Java Java的方式URLencode.encode("","UTF-8")
展开

是的,StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);是这句出问题了,不应该选择默认编码,应该改为treamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));

0
#3goJhou回复于2018-04-27

streamreader选错编码了

1
#2756665228回复于2018-04-27

 HttpUtility.UrlEncode(strbaser64); 你这里需要加UTF-8。

之前一个C#也是遇到这样的问题。说加了UTF-8就好了。具体加哪里我也不清楚。我是Java

Java的方式URLencode.encode("","UTF-8")

1
TOP
切换版块