1. 程式人生 > >自己封裝的java傳送簡訊通用工具類

自己封裝的java傳送簡訊通用工具類

複製直接使用 不懂的可以下方留言

//工具類
package com.picc.inquiries.util;

/**
 * @author 張崇俊
 * @create 2018-08-09 15:03
 * @descriptions <p></p >
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
    // 建立HttpClient物件
    public static HttpClient httpClient = new DefaultHttpClient();
    public static final String BASE_URL = "url";
    /**
     *
     * @param url 傳送請求的URL
     * @return 伺服器響應字串
     * @throws Exception
     */
    public static String getRequest(final String url)
            throws Exception
    {
        FutureTask<String> task = new FutureTask<String>(
                new Callable<String>()
                {
                    public String call() throws Exception
                    {
                        // 建立HttpGet物件。
                        HttpGet get = new HttpGet(url);
                        // 傳送GET請求
                        HttpResponse httpResponse = httpClient.execute(get);
                        // 如果伺服器成功地返回響應
                        if (httpResponse.getStatusLine()
                                .getStatusCode() == 200)
                        {
                            // 獲取伺服器響應字串
                            String result = EntityUtils
                                    .toString(httpResponse.getEntity());
                            return result;
                        }
                        return null;
                    }
                });
        new Thread(task).start();
        return task.get();
    }

   /* public static void main(String[] args) throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("phone","手機號");
        String s = postRequest("伺服器名稱", map);
        System.out.println(s);
    }*/

    public static void main(String[] args) throws Exception {
        Map<String,String> map = new HashMap<String, String>();
        String tkey=TimeUtil.getNowTime("yyyy-MM-dd HH:mm:ss");
        map.put("login_name","picchens");
        map.put("password","apegTo7E");
        map.put("mobile","手機號");
        map.put("message","驗證碼為");
        map.put("start_time",tkey);
        map.put("Search_ID","-1");
        String s = postRequest("伺服器名稱", map);
        System.out.println(s);
    }
    
    /**
     * @param url 傳送請求的URL
     * @return 伺服器響應字串
     * @throws Exception
     */
    
    public static String postRequest(final String url, final Map<String ,String> rawParams)throws Exception {
        FutureTask<String> task = new FutureTask<String>(
                new Callable<String>()
                {
                    public String call() throws Exception
                    {
                        // 建立HttpPost物件。
                        HttpPost post = new HttpPost(url);
                        // 如果傳遞引數個數比較多的話可以對傳遞的引數進行封裝
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        for(String key : rawParams.keySet())
                        {
                            //封裝請求引數
                            params.add(new BasicNameValuePair(key
                                    , rawParams.get(key)));
                        }
                        // 設定請求引數
                        post.setEntity(new UrlEncodedFormEntity(
                                params, "gbk"));
                        // 傳送POST請求
                        HttpResponse httpResponse = httpClient.execute(post);
                        // 如果伺服器成功地返回響應
                        if (httpResponse.getStatusLine()
                                .getStatusCode() == 200)
                        {
                            // 獲取伺服器響應字串
                            String result = EntityUtils
                                    .toString(httpResponse.getEntity());
                            return result;
                        }
                        return null;
                    }
                });
        new Thread(task).start();
        return task.get();
    }
}

原創共同學習進步