码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-07-29 23:06:10      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:new   val   edr   pair   调用   sage   str   system   message   

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

标签:new   val   edr   pair   调用   sage   str   system   message   

原文地址:https://www.cnblogs.com/H-Dream/p/11267053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!