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

asp.net 发送数据到短信接口的问题。急!
我公司在万网买了一个企业短信的API版的短信接口。他们提供的文档上有asp,php,jsp的例子。就是没有asp.net的。
asp版本的代码为:
<%
function send_sms(user_id,password,mobile_phone,msg,sendtime,subcode)
  dim sendStr,http
  Err.Clear
  On Error Resume Next
  sendStr="user_id=" & user_id & "&password=" & password & "&mobile_phone=" & mobile_phone & "&msg=" & server.URLEncode(msg) & "&sendtime=" & sendtime & "&subcode=" & subcode
  set http=server.CreateObject("WinHttp.WinHttpRequest.5.1")
  http.SetTimeouts 30000,50000,30000,30000
  http.Open "POST", "http://bms.hichina.com/sms_gateway/sms_api", False
  http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"  

  http.Send sendStr  

  If Err.Number = 0 Then 
  send_sms=trim(http.responsetext)
  else
  send_sms=-1
  Response.Write Err.Number 
  end if
  http.Abort()
end function
dim ret
ret = send_sms ("1111","111111","139xxxxxxx","测试短信发送内容","","1111")//参数分别为 用户名,密码,手机号,内容,发送时间,默认为即时,企业代码和用户名相同。
Response.Write ret
%>

=========
我对asp不是很熟悉,这个代码可以用,但我想写成.net的。是不是要用webrequest?
我写的代码如下:(其中一些参数都随便写的。)
 private static void HttpPost()
  {
  string postData = "user_id=1111&passwrod=111111&mobile_phone=139xxxx&msg=1111&sendtime=''&subocde=1111";//在发送的内容里包括中文和空格
  Encoding encoding = Encoding.GetEncoding("GB2312");
  string strUrl = "http://bms.hichina.com/sms_gateway/sms_api";
  byte[] data = encoding.GetBytes(postData);
  // 准备请求...
  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
  myRequest.Method = "POST";
  myRequest.ContentType = "application/x-www-form-urlencoded";
  myRequest.ContentLength = data.Length;
  Stream newStream = myRequest.GetRequestStream();
  // 发送数据
  newStream.Write(data, 0, data.Length);
  newStream.Close();
  }

=====
但是我运行代码没有反应,请帮忙看看我的错在哪里了,着急啊。谢谢

------解决方案--------------------
private bool Send(string url, string sendData)
{
string ecode = sendData;
try
{
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

string strQuery = ecode;
string dataSend = strQuery;
req.ContentLength = dataSend.Length;

byte[] buff = Encoding.Default.GetBytes(dataSend);
Stream reqStream = req.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
reqStream.Close();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
//Log.LogSave("[" + DateTime.Now.ToString() + "]" + ex.Message + "\r\n");
return false;
}
}
------解决方案--------------------
那你需要联系万网,看是什么情况
要他们技术查下是否调用了这个接口。
------解决方案--------------------