HttpClient模拟x-www-form-urlencoded格式的form表单提交
2021-01-04 03:28
阅读:664
标签:for pac username sea entity def color ble eva
form表单提交的时候,是x-www-form-urlencoded 格式,此时如果用HttpClient类去做,代码应该是这样:
import com.gewara.util.HttpResult;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
// 组织参数 TreeMaptreeMapParams = new TreeMap(); treeMapParams.put("username", "xx"); List nameValuePairList = OpenapiParamsUtil.buildNameValuePairList(treeMapParams); // 设置参数 HttpPost httpPost = new HttpPost(HttpUtil.LOCK_COUPON_URL); UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8"); httpPost.setEntity(urlEncodedFormEntity); HttpResult httpResult = null; try( CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); ){ httpResult = HttpUtil.dealResponse(response); String result = httpResult.getResponse(); logger.info("result ============= " + result); return result; } catch (Exception e){ logger.error(e.getMessage() ,e); }
添加依赖jar:
dependency>
groupId>commons-httpclientgroupId>
artifactId>commons-httpclientartifactId>
version>3.1version>
dependency>
end.
HttpClient模拟x-www-form-urlencoded格式的form表单提交
标签:for pac username sea entity def color ble eva
原文地址:https://www.cnblogs.com/zhuwenjoyce/p/13631583.html
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:HttpClient模拟x-www-form-urlencoded格式的form表单提交
文章链接:http://soscw.com/essay/39956.html
文章标题:HttpClient模拟x-www-form-urlencoded格式的form表单提交
文章链接:http://soscw.com/essay/39956.html
评论
亲,登录后才可以留言!