短信平台接口調用方法參考


C#代碼示例

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作相應處理
            }

PHP代碼示例

http請求
<?php

$urlsend="http://xxx.com/api/MsgSend.asmx/sendMes";

$token=array("userCode"=>"用戶名","userPass"=>"密碼","DesNo"=>"手機號","Msg"=>"短信內容【簽名】","Channel"=>"通道號");

echo http($urlsend,$token,"GET"); //get請求

echo http($urlsend,$token,"POST"); //post請求

function http($url,$param,$action="GET"){
    $ch=curl_init();
    $config=array(CURLOPT_RETURNTRANSFER=>true,CURLOPT_URL=>$url);    
    if($action=="POST"){
        $config[CURLOPT_POST]=true;        
    }
    $config[CURLOPT_POSTFIELDS]=http_build_query($param);
    curl_setopt_array($ch,$config);    
    $result=curl_exec($ch);    
    curl_close($ch);
    return $result;
}
?>

webservice請求

<?php
    //此處僅示例發送短信,其他可類推
    header("Content-type: text/html; charset=utf-8");
    $client = new SoapClient("http://xxx.com/api/MsgSend.asmx?WSDL");
    $param = array("userCode"=>"用戶名","userPass"=>"密碼","DesNo"=>"手機號","Msg"=>"短信內容【簽名】","Channel"=>"通道號");
    $p = $client->sendMes($param);
    print_r($p);
    ?>

java代碼示例

http請求
//說明:此處需引用httpclient、httpcore、commons-logging三個jar包

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.util.*;
        import java.security.MessageDigest;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.client.entity.UrlEncodedFormEntity;
        import org.apache.http.impl.client.DefaultHttpClient;
        import org.apache.http.message.BasicNameValuePair;
        import org.apache.http.*;
        import javax.crypto.SecretKey;
        import javax.crypto.spec.DESKeySpec;
        import javax.crypto.spec.IvParameterSpec;
        import javax.crypto.SecretKeyFactory;
        import javax.crypto.Cipher;

        public static void main(String[] args) {
            String url="http://xxx.com/api/MsgSend.asmx/SendMes";

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("userCode", "用戶名"));
            nvps.add(new BasicNameValuePair("userPass", "密碼"));
            nvps.add(new BasicNameValuePair("DesNo", "手機號"));
            nvps.add(new BasicNameValuePair("Msg", "短信內容【簽名】"));
            nvps.add(new BasicNameValuePair("Channel", "通道號"));
            String post=httpPost(url,nvps);  //post請求

            String getparam="userCode=用戶名&userPass=密碼&DesNo=手機號&Msg=短信內容【簽名】&Channel=通道號";
            String result=httpGet(url,getparam); //get請求
        }

    public static String httpPost(String url,List<NameValuePair> params) {
            String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);            
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {    
                 InputStream instreams = entity.getContent();    
                 result = convertStreamToString(instreams);  
                 System.out.println(result);  
             }
        } catch (Exception e) {
        }
        return result;
    }
    
    public static String httpGet(String url,String params){
        String result="";        
        try{
            HttpClient client=new DefaultHttpClient();            
            if(params!=""){
                url=url+"?"+params;
            }            
            HttpGet httpget=new HttpGet(url);
            HttpResponse response=client.execute(httpget);            
            HttpEntity entity=response.getEntity();
            if (entity != null) {    
                 InputStream instreams = entity.getContent();    
                 result = convertStreamToString(instreams);  
                 System.out.println(result);  
             }
        }catch(Exception e){}
        return result;
    }
    
    public static String convertStreamToString(InputStream is) {      
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));      
        StringBuilder sb = new StringBuilder();      
       
        String line = null;
        try {      
            while ((line = reader.readLine()) != null) {  
                sb.append(line + "\n");      
            }      
        } catch (IOException e) {      
            e.printStackTrace();      
        } finally {
            try {
                is.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
        return sb.toString();
    }
         

webservice請求

 public static void main(String[] args) {
            org.tempuri.MsgSend service = new org.tempuri.MsgSend();
            org.tempuri.MsgSendSoap port = service.getMsgSendSoap();
            String result= port.sendMes("用戶名","密碼","手機號","短信內容【簽名】","通道號");
            System.out.println(result);
        }

asp代碼示例

http請求

  <%
dim sendurl,senddata
sendurl="http://xxx.com/api/MsgSend.asmx/SendMes"
senddata="userCode=用戶名&userPass=密碼&DesNo=手機號&Msg=短信內容【簽名】&Channel=通道號"

Response.Write(HTTPRequest(sendurl,senddata,"GET")) <!-- get請求 -->

Response.Write(HTTPRequest(sendurl,senddata,"POST")) <!-- post請求 -->


function HTTPRequest(url,data,method)
    dim http
    set http=server.createobject("MSXML2.SERVERXMLHTTP.3.0")

    if method = "GET" then
    Http.open "GET",url+"?"+data,false
    elseif method = "POST" then
    Http.open "POST",url,false
    end if

    Http.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded"
    Http.send(data)
    if Http.readystate<>4 then
    exit function
    End if

    HTTPRequest=BytesToStr(Http.responseBody,"utf-8")
    set http=nothing
    if err.number<>0 then err.Clear
End function

Function BytesToStr(body, charset)
    Dim objStream
    Set objStream = Server.CreateObject("Adodb.Stream")
    objStream.Type = 1
    objStream.Mode = 3
    objStream.Open
    objStream.Write body
    objStream.Position = 0
    objStream.Type = 2
    objStream.Charset = charset
    BytesToStr = objStream.ReadText 
    objStream.Close
    Set objStream = Nothing
End Function

%>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM