移动网关发送短信,接到乱码

loveness 2012-10-29 11:13:00
用的是移动网关,直接在ie浏览器发送,手机接收正常,可显示中英文
用代码实现,可以收到剩余条数,说明网路是通的,如果发送中文内容,手机收到一大串乱码,最后一个数字为0,
源代码如下
public string PostUrl(String aUrl)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(aUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding);
string htmlstr = reader.ReadToEnd();
reader.Close();
response.Close();
return htmlstr;
}

public bool sendSMS( string username, string regicode,string mobile)
{
System.DateTime da = new DateTime();
da = System.DateTime.Now;
int d = Convert.ToInt32(da.Hour.ToString());
if (d < 18 && d > 8) //工作时间内发送
{
string password = "XXXXX";
string user_name = "XXXXX";
string content = "";

string posturl9 = "";
posturl9 = "http://jk.92c2.com/userinfo.asp?userid=" + user_name + "&userpass=" + password;

string texts = PostUrl(posturl9); //剩余短信条数
if (Convert.ToInt32(texts) > 0)
{
posturl9 = "http://jk.92c2.com/send.asp?userid=" +
user_name +"&userpass=" + password + "&urllongsms=0" +
"&content=" +
"尊敬的" + username +":您申请的注册欣是:" + Convert.ToString(RandKey) +
//Server.UrlDecode("尊敬的" + username + ":您申请的注册码是:XXX" +
"&mobile=" + mobile;
string re = PostUrl(posturl9);
if (re == "0")
{
//发送成功
}
}

}
return true;
}

调试过程:
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
把这里的gb2312改成utf-8,一样乱码

把短信内容 "尊敬的" + username +":您申请的注册欣是:" + Convert.ToString(RandKey)
用Server.UrlDecode编码一下,都是一样的乱码

求解
...全文
412 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_20599885 2014-10-17
  • 打赏
  • 举报
回复
loveness 在么?这个问题解决了吗?是haukwong兄弟给的答案做成的吗?求解!
loveness 2012-11-01
  • 打赏
  • 举报
回复
haukwong兄弟真是耐心人,接分
Hauk 2012-10-31
  • 打赏
  • 举报
回复

public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
{
cookieContainer = cookieContainer ?? new CookieContainer();

HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;

try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;

if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.GetEncoding("gbk").GetBytes(postData);//使用GBK
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
}

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();

streamReader.Close();
responseStream.Close();

httpWebRequest.Abort();
httpWebResponse.Close();

return html;
}
catch (Exception)
{
return string.Empty;
}
}

string url = "http://jk.92c2.com/send.asp";
HttpHelper.GetHtml(url, null, "POST", "userid=meifa&userpass=888888&urllongsms=0&content=测试1234get&mobile=13800138000");


设置请求编码为GBK就可以了。

Hauk 2012-10-31
  • 打赏
  • 举报
回复
sorry没有贴完整:
httpWebRequest.ContentType = "application/x-www-form-urlencoded";//请求内容格式
httpWebRequest.Referer = url;//请求来源路径
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";//指明该请求能够接收的类型
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";//封装了客户端的一些用户信息


这些值基本上都不怎么变的,可以通过拦截http请求查看到。
loveness 2012-10-31
  • 打赏
  • 举报
回复
后面 httpWebRequest.Method = method; 那句,源代码是没有被注释的,黏贴上来格式错位了。
loveness 2012-10-31
  • 打赏
  • 举报
回复
haukwong 兄弟:我对c#.net不是很熟,半路出家的,根据你的代码,我封装了一类类,然后调用,返回错误执行结果,错误代码是-2,“内容或号码为空”
由于这四句不是道是啥,编译报错,就屏蔽了,完整代码如下,还请多多指教,看看哪里有问题
sendSMS类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Hair.Entity
{
public partial class SendSMS
{
public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
{
cookieContainer = cookieContainer ?? new CookieContainer();

HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;

try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
//httpWebRequest.ContentType = contentType;
//httpWebRequest.Referer = referer;
//httpWebRequest.Accept = accept;
//httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;

if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.GetEncoding("gbk").GetBytes(postData);//使用GBK
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
}

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();

streamReader.Close();
responseStream.Close();

httpWebRequest.Abort();
httpWebResponse.Close();

return html;
}
catch (Exception)
{
return string.Empty;
}
}
}
}

调用代码:
SendSMS ss = new SendSMS();
string url = "http://jk.92c2.com/send.asp";
TextBox13.Text = SendSMS.GetHtml(url, null, "POST", "userid=meifa&userpass=888888&urllongsms=0&content=测试1234get&mobile=13719438018");
loveness 2012-10-31
  • 打赏
  • 举报
回复
haukwong 兄弟:可在
加了一个测试号码供调试,
http://jk.92c2.com/send.asp?userid=meifa&userpass=888888&urllongsms=0&content=测试123&mobile=13719438018
说明:urllongsms=0,表示短短信(76个字符以内),content 发送内容,mobile接收手机
只能在早上8.30到 下午六点使用
这条语句放在浏览器,执行,完全正常,
Hauk 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

to:1、2楼兄弟:
细看一下,你的写法和我的源代码,木有本质区别,只是变量名换了而已,测试无效
3楼兄弟 用encode乱码依旧
4楼兄弟,原题已说,utf-8不行
5、6楼兄弟, 根据你的提议;加了request.Headers.Add("content", "text/html; charset=gb2312"); 问题如故
posturl加了编码,
HttpWebReques……
[/Quote]

用Uri.EscapeUriString(posturl9)来搞,
用Server.UrlEncode来编码整个串的话是会出问题
loveness 2012-10-29
  • 打赏
  • 举报
回复
配置文件?Web.Config这个还是sms的配置文件
Web.Config,系统默认的,没动guo
sms木有配置文件,直接post提交的,参数都是通过url传递的
EnForGrass 2012-10-29
  • 打赏
  • 举报
回复
配置文件怎么写的??
loveness 2012-10-29
  • 打赏
  • 举报
回复
to:1、2楼兄弟:
细看一下,你的写法和我的源代码,木有本质区别,只是变量名换了而已,测试无效
3楼兄弟 用encode乱码依旧
4楼兄弟,原题已说,utf-8不行
5、6楼兄弟, 根据你的提议;加了request.Headers.Add("content", "text/html; charset=gb2312"); 问题如故
posturl加了编码,
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Server.UrlEncode(aUrl));
执行反而出错,连以前可以执行的查询剩余条数都出错,出错信息“无效的 URI: 无法确定 URI 的格式。”跟踪url,拿出来,放到ie地址栏,正确
Hauk 2012-10-29
  • 打赏
  • 举报
回复
还有在你PostUrl中是用什么编码把post数据写入RequestStream的呢?
这几个地方都可以试一下
Hauk 2012-10-29
  • 打赏
  • 举报
回复
Request的时候用UrlEncode

或者在你PostUrl方法中设置httpRequest.Headers.Add("content", "text/html; charset=gb2312");
nikolaichow 2012-10-29
  • 打赏
  • 举报
回复
UTF-8格式
来一脚 2012-10-29
  • 打赏
  • 举报
回复
encode是编码,你用的Server.UrlDecode是解码
XBodhi. 2012-10-29
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader (stream,encoding)
sr.ReadToEnd();
XBodhi. 2012-10-29
  • 打赏
  • 举报
回复
你用 response.Charsets 吧 你接受的 编码重新 编码下 就好了。
Hauk 2012-10-29
  • 打赏
  • 举报
回复
给一个出来嘛,我来看看呢。
loveness 2012-10-29
  • 打赏
  • 举报
回复
问题未解决,继续求解
loveness 2012-10-29
  • 打赏
  • 举报
回复
如果有必要,我可以开一个临时SMS用户供大家调试使用
加载更多回复(1)

110,535

社区成员

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

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

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