问题描述
C#通过HttpWebRequest和HttpWebResponse请求和获取短信接口信息,提交相关短信内容和接收手机号码,获取提交状态。以下代码参考了速达移动(sudas.cn)接口样例。Post.aspx.cs源码:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.IO;usingSystem.Net;usingSystem.Text;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassPost:System.Web.UI.Page{publicstaticstringPostUrl=ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButSubmit_Click(objectsender,EventArgse){stringsname=this.TxtSname.Text.Trim();stringspwd=this.TxtSpwd.Text.Trim();stringscorpid=this.TxtScorpid.Text.Trim();stringsprdid=this.TxtSprdid.Text.Trim();stringsdst=this.TxtSdst.Text.Trim();stringsmsg=this.TxtSmsg.Text.Trim();stringpostStrTpl="sname={0}&spwd={1}&scorpid={2}&sprdid={3}&sdst={4}&smsg={5}";UTF8Encodingencoding=newUTF8Encoding();byte[]postData=encoding.GetBytes(string.Format(postStrTpl,sname,spwd,scorpid,sprdid,sdst,smsg));HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(PostUrl);myRequest.Method="POST";myRequest.ContentType="application/x-www-form-urlencoded";myRequest.ContentLength=postData.Length;StreamnewStream=myRequest.GetRequestStream();//Sendthedata.newStream.Write(postData,0,postData.Length);newStream.Flush();newStream.Close();HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();if(myResponse.StatusCode==HttpStatusCode.OK){StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.UTF8);LabelRetMsg.Text=reader.ReadToEnd();//反序列化upfileMmsMsg.Text//实现自己的逻辑}else{//访问失败}}}