分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

httpclient x-www-form-urlencoded

发布时间:2023-09-06 01:38责任编辑:胡小海关键词:urlhttp

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

原文地址:https://www.cnblogs.com/rocky-fang/p/8329025.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved