关于C# 转BASE64 在UTF-8编码 POST提交给JAVA 接口的时候 提示stream.length异常的问题

three1983 2019-03-29 10:38:44
目前我的C/S程序要 提交给java做的一个接口数据,json字符串,然后通过BASE64加密 通过http Post提交,提交的提示stream.length引发了system.notsuporttedexception类型的异常
代码如下:

string url = "http://xxxxxxx/websms/thirdMsg.do?method=execute";
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
webrequest.Method = "post";
webrequest.ContentType = "application/json";
string coderstr = "\"code\":\"test\"";
string passwordstr = "\"password\":\"111111\"";
string content = "\"content\":\"测试!\"";
string mobile = "\"mobile\";[\"" + dt.Rows[0]["Mobile"].ToString() + "\"]";
string commType = "\"commType\":\"1\"";
string replyFlag = "\"replyFlag\":\"0\"";
string replyTime = "\"replyTime\":\"" + Tools.GetDate().ToString("yyyyMMddHHmmss") + "\"";
string notEncrypting = "\"notEncrypting\"\"0\"";

string paramStr = "{" + coderstr + "," + passwordstr + "," + content + "," + mobile + "," + commType + "," + replyFlag + "," + replyTime + "," + notEncrypting + "}";
byte[] b = System.Text.Encoding.Default.GetBytes(paramStr);
string aa = Convert.ToBase64String(b);
byte[] ss = Encoding.UTF8.GetBytes(aa);
webrequest.ContentLength = ss.Length;
Stream stream;
stream = webrequest.GetRequestStream();
stream.p(ss, 0, ss.Length);
stream.Close();
...全文
405 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
three1983 2019-03-29
  • 打赏
  • 举报
回复
引用 4 楼 stherix的回复:
[quote=引用 3 楼 three1983 的回复:] [quote=引用 2 楼 stherix的回复:]另外推荐用WebClient 类 比起HttpWebRequest要方便
请问我这样转base64的步骤是否正确[/quote] 转base64就是这样,是个简单的过程 不过你上面用了System.Text.Encoding.Default.GetBytes(paramStr); 这个要看java端是否也是用的GB编码,不然base64转回去中文会是乱码,可能也要改成UTF8[/quote] 明白了,谢谢,我试试,万分感谢
stherix 2019-03-29
  • 打赏
  • 举报
回复
引用 3 楼 three1983 的回复:
[quote=引用 2 楼 stherix的回复:]另外推荐用WebClient 类 比起HttpWebRequest要方便
请问我这样转base64的步骤是否正确[/quote] 转base64就是这样,是个简单的过程 不过你上面用了System.Text.Encoding.Default.GetBytes(paramStr); 这个要看java端是否也是用的GB编码,不然base64转回去中文会是乱码,可能也要改成UTF8
three1983 2019-03-29
  • 打赏
  • 举报
回复
引用 2 楼 stherix的回复:
另外推荐用WebClient 类 比起HttpWebRequest要方便
请问我这样转base64的步骤是否正确
stherix 2019-03-29
  • 打赏
  • 举报
回复
另外推荐用WebClient 类 比起HttpWebRequest要方便
stherix 2019-03-29
  • 打赏
  • 举报
回复
post请求 你不需要手动指定ContentLength的大小 只要往stream里写入你的数据就可以了

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧