日期:2014-05-17  浏览次数:20939 次

移动网关发送短信,接到乱码
用的是移动网关,直接在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编码一下,都是一样的乱码

求解

------解决方案--------------------
Request的时候用UrlEncode

或者在你PostUrl方法中设置httpRequest.Headers.Add("content", "text/html; charset=gb2312");