• httpclient x-www-form-urlencoded


    1. 使用Apache httpclient提交post请求

    http工具方法(需指定编码, 否则出错,这里用的UTF-8)

    public static String postWithParamsForString(String url, List<NameValuePair> params){
            HttpClient client = HttpClients.createDefault();
            HttpPost httpPost =   new HttpPost(url);
            String s = "";
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
                httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); 
                HttpResponse response = client.execute(httpPost);
                int statusCode = response.getStatusLine().getStatusCode();
                if(statusCode==200){
                    HttpEntity entity = response.getEntity();
                    s = EntityUtils.toString(entity);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }

    测试方法

    public static void main(String[] args) {
    
            
            String smsKey = "aaaaaaa";
            String content = "您的订单号是:4322311";
            String phone = "111111";
            String smsSecret = "bbbb";
            String smsUrl = "http://iccc/v1/message/content/send";
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            String timestamp = String.valueOf(System.currentTimeMillis());
            String signContent = "appkey=" + smsKey + "&content=" + content + "&mobile=" + phone + "&timestamp="
                    + timestamp + "&appsecret="+ smsSecret;
            String sign = DigestUtils.md5Hex(signContent).toUpperCase();
            NameValuePair pair = new BasicNameValuePair("appkey", smsKey);
            NameValuePair pair2 = new BasicNameValuePair("content", content);
            NameValuePair pair3 = new BasicNameValuePair("mobile", phone);
            NameValuePair pair4 = new BasicNameValuePair("timestamp", timestamp);
            NameValuePair pair5 = new BasicNameValuePair("sign", sign);
            params.add(pair);
            params.add(pair2);
            params.add(pair3);
            params.add(pair4);
            params.add(pair5);
            String postForString = HttpUtil.postWithParamsForString(smsUrl, params);
    }

    2. okhttp 实现

    import okhttp3.*;
    import org.apache.commons.codec.digest.DigestUtils;
    
    import java.io.IOException;
    import java.util.concurrent.TimeUnit;
    
    /**
     * Created by admin on 2018/1/22.
     */
    public class SmsSender {
    
        private  static final OkHttpClient client = new OkHttpClient.Builder().
                connectionPool(new ConnectionPool(100,10, TimeUnit.MINUTES))
                .connectTimeout(5,TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS).build();
    
        public static String postFormBody(String url, FormBody body){
            Request request = new Request.Builder().url(url)
                    .post(body).build();
            try {
                Response response = client.newCall(request).execute();
                return response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }
    
        public static void main(String[] args) throws IOException {
            String content = "您的订单号是:4322311";
            String mobile = "1314455";
            String timestamp = String.valueOf(System.currentTimeMillis());
            String signContent = "cc";
            String sign = DigestUtils.md5Hex(signContent).toUpperCase();
            FormBody body = new FormBody.Builder()
                    .add("content",content).add("mobile",mobile)
                    .add("timestamp",timestamp).add("sign",sign)
                    .build();
            String url = "dd/v1/message/content/send";
            String result = postFormBody(url, body);
            System.out.println(result);
        }
    }

    3. postman 测试

    选中x-www-form-urlencoded 输入相应key value

  • 相关阅读:
    spring中bean的生命周期
    【数据结构与算法】2.2 数组实现循环队列思路、代码
    【数据结构与算法】2.1、数组队列场景、思路、实现
    【Java 基础领域】手气红包实现思路、代码
    【数据结构与算法】1、稀疏数组场景、思路、代码实现
    【Java基础领域】 byte num = 100 没有强制类型转换,为什么也可以编译通过
    【程序人生】程序员发展的7大方向
    【读书笔记】老许的架构
    对于开发中为什么很少用设计模式的思考
    Java编程思想目录
  • 原文地址:https://www.cnblogs.com/rocky-fang/p/8329025.html
Copyright © 2020-2023  润新知