http请求
string url="http://xxx.com/api/MsgSend.asmx"; protected string sendmsgByPost() //POST方式请求 { StringBuilder sb = new StringBuilder(); sb.Append("userCode=用户名&"); sb.Append("userPass=密码&"); sb.Append("DesNo=手机号&"); sb.Append("Msg=短信内容【签名】&"); sb.Append("Channel=通道号"); string result = httpPost(url + "/sendMes", sb.ToString()); return result; } protected string sendmsgByGet() //Get方式请求 { StringBuilder sb = new StringBuilder(); sb.Append("userCode=用户名&"); sb.Append("userPass=密码&"); sb.Append("DesNo=手机号&"); sb.Append("Msg=短信内容【签名】&"); sb.Append("Channel=通道号"); string result = httpGet(url + "/sendMes", sb.ToString()); return result; } protected string httpGet(string url, string data) //http get请求 { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + data); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { return ex.Message; } } protected string httpPost(string url, string data) //http post请求 { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; byte[] info = Encoding.UTF8.GetBytes(data); using (Stream stream = request.GetRequestStream()) { stream.Write(info, 0, info.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { return ex.Message; } }
webservice请求
private void SendSms() //webservice请求方式 { TopenServiceReference.MsgSendSoapClient topen = new TopenServiceReference.MsgSendSoapClient(); string userName = "用户名"; string passWord = "密码"; string mobiles = "13900000000,13800000000,13100000000,……"; string msgContent = "短信内容(含签名)"; string channel = "由拓鹏给您的通道编号"; string sendResult = topen.sendMes(userName, passWord, mobiles, msgContent, channel); //此处的sendMes可能因接口文档不同而不同,请注意。返回批次号,可保存下来,作为获取发送报告凭据 //然后,根据返回的sendResult作相应处理 }