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

转载于:https://www.cnblogs.com/rocky-fang/p/8329025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值