短信平台接口调用方法(java语言)

Posted h-dream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了短信平台接口调用方法(java语言)相关的知识,希望对你有一定的参考价值。

//说明:此处需引用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();
    

 

以上是关于短信平台接口调用方法(java语言)的主要内容,如果未能解决你的问题,请参考以下文章

java调用微信公众平台接口

java程序怎么调用webservice接口,实现发送短信功能

java调用webservice接口实现发送短信??????

java调用阿里云短信接口

java怎么调用别人给的接口

java远程调用接口的原理和范例,谢谢