问题描述
微信卡券json写入流的字节超出指定的Content-Length字节大小。post方法如下:publicstaticstringHttpPost(stringUrl,stringpostDataStr){HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(Url);request.Method="POST";request.ContentType="application/json;encoding=utf-8";//request.ContentLength=postDataStr.Length;StreamWriterwriter=newStreamWriter(request.GetRequestStream());//,Encoding.ASCIIwriter.Write(postDataStr);writer.Flush();HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();stringencoding=response.ContentEncoding;if(encoding==null||encoding.Length<1){encoding="UTF-8";//默认编码}StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.GetEncoding(encoding));stringretString=reader.ReadToEnd();returnretString;}json如下:[{"card":{"card_type":"GROUPON","groupon":{"base_info":{"logo_url":"http://mmbiz.qpic.cn/mmbiz/pEjibTibnM4kgznOPpznicMmVj1LdEicANxicQA3G3OZwzDwCxLy31f9Jt9tOwLZxBGP1naM055QestHkTuxCVwibH8w/0","brand_name":"测试","code_type":"CODE_TYPE_QRCODE","title":"测试","sub_title":"测试","color":"Color010","notice":"三日内使用","service_phone":"10000","description":"三日内使用","date_info":{"type":"DATE_TYPE_FIX_TIME_RANGE","begin_timestamp":1472724261,"end_timestamp":1502724261},"sku":{"quantity":50},"get_limit":1,"use_custom_code":false,"bind_openid":false,"can_share":true,"can_give_friend":true,"location_id_list":[133],"custom_url_name":"立即使用","custom_url":"baid.com","custom_url_sub_title":"立即享受优惠","promotion_url_name":"产品介绍","promotion_url":"baid.com","source":"大众点评"},"deal_detail":"双人套餐\n-进口红酒一支。\n孜然牛肉一份。"}}}]如果注释掉request.ContentLength=postDataStr.Length;会报json解析错误,用微信网页调试json绝对没有问题的。
解决方案
本帖最后由 yeyelishuang 于 2016-04-16 14:00:43 编辑
解决方案二:
已解决,[]出问题了,post过去带[]的不行。网页调试可以带,真坑
解决方案三:
呵呵只传【里面的数据】否则会报错
解决方案四:
那边要求的不是json格式吧,网页格式可以估计是转成了json
解决方案五:
ContentLength表示utf8编码的字节数,你那个是什么?
解决方案六:
Utf8或者是别的什么编码,是你的ContentEncoding所一致的编码。关键它是标识的是字节数,而你那个写的是unicode编码的字符数,字符数绝对比字节数小很多啊!