1. 程式人生 > >httpclient x-www-form-urlencoded

httpclient x-www-form-urlencoded

exception cas sage mobile pool bbb return pub exce

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