httpclient x-www-form-urlencoded

Posted rocky-fang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. post 测试

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

以上是关于httpclient x-www-form-urlencoded的主要内容,如果未能解决你的问题,请参考以下文章

get和post的区别

轻松把玩HttpClient之封装HttpClient工具类,插件式配置HttpClient对象

Httpclient4.5.*HttpClient请求,对于新建httpclient实例时保持会话

postman使用--接口的关联

HttpClient学习整理

httpclient简介说明