【httpclient模拟post(applecation/x-www-form-urlencoded方式)请求】一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述httpclient模拟post(applecation/x-www-form-urlencoded方式)请求相关的知识,希望能为你提供帮助。
http post请求工具类:
/** * 非json参数方式POST提交 * * @param url * @param params * @return */ public static String httpPost(String uri, List< NameValuePair> params) { String result = ""; try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 添加请求头 httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); System.out.println(httpPost); HttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); result = convertStreamToString(instreams); System.out.println(result); } } catch (Exception e) { e.getMessage(); } return result; }public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + " "); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
请求数据组装:
public R authenticationInfo(AuthenticationInfoEntity authenticationInfo) {List< NameValuePair> nvps = new ArrayList< NameValuePair> (); nvps.add(new BasicNameValuePair("apiCode", "P203")); nvps.add(new BasicNameValuePair("account", "demo")); nvps.add(new BasicNameValuePair("password", "UGFpbGllQDIwMTk=")); nvps.add(new BasicNameValuePair("name", authenticationInfo.getAiFullName())); nvps.add(new BasicNameValuePair("cid", authenticationInfo.getAiIdCard())); nvps.add(new BasicNameValuePair("bankCard", authenticationInfo.getAiBankAccount())); nvps.add(new BasicNameValuePair("mobile", authenticationInfo.getAiMobile())); String jsonStr = httpPost(uri, nvps); //post请求 }
推荐阅读
- 解决AndroidKiller APK 反编译失败,无法继续下一步源码反编译!
- 安卓无法上传图片的问题
- AppBoxFuture: Raft快照及日志截断回收
- 关于appium的简单理解
- Oracle 日志分析 APP | APP 推荐第 6 弹
- PowerShell set-location| PowerShell更改目录
- PowerShell Set-Content设置内容| PowerShell写入文件
- PowerShell Rename-Item| PowerShell重命名文件
- PowerShell Remove-Item| PowerShell删除文件