热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

httpclient的几种post参数格式

1、json格式HttpClientContextcontextHttpClientContext.create();httpPost.setU

1、json格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost.setURI(java.net.URI.create(url));

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}

StringEntity entity1 = new StringEntity(json, HTTP.UTF_8);
httpPost.setEntity(entity1);
entity1.setContentType("application/json");


System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();

entity.getContent().close();
}

2、urlencoded格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost.setURI(java.net.URI.create(url));

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}


StringEntity entity1 = new StringEntity(postDataEncode, HTTP.UTF_8);

entity1.setContentType("application/x-www-form-urlencoded");

httpPost.setEntity(entity1);

System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();

entity.getContent().close();
}

3、key-value格式

HttpClientContext cOntext= HttpClientContext.create();

httpPost = new HttpPost(url);

if(null != headers) {
for (String name : headers.keySet()) {
httpPost.setHeader(name, headers.get(name));
}
}

if(null != params) {
List nvps = new ArrayList();
for (String name : params.keySet()) {
nvps.add(new BasicNameValuePair(name, params.get(name)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
}


System.out.println("Executing request " + httpPost.getRequestLine() + " to " + " via " + proxy);

CloseableHttpResponse respOnse= httpClient.execute(httpPost,context);
response.setLocale(Locale.CHINESE);

String redirectUrl = "";
List redirectUrls = context.getRedirectLocations();
if(redirectUrls!=null){
redirectUrl = redirectUrls.get(0).toString();
System.out.println("重定向:" + redirectUrl);
}

long end = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");
// System.out.println(EntityUtils.toString(response.getEntity()));

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
entity = response.getEntity() ;

String html = EntityUtils.toString(entity, charSet);
response.close();
// httpGet.releaseConnection();

entity.getContent().close();

}

4、MultipartForm

 CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");

FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("bin", bin)
.addPart("comment", comment)
.build();


httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());
CloseableHttpResponse respOnse= httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

5、XML格式

 File input = new File(“test.xml”);
PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);

// 设置请求的内容直接从文件中读取
post.setRequestBody( new FileInputStream(input));
if (input.length() .MAX_VALUE)
post.setRequestContentLength(input.length());
else
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

// 指定请求内容的类型
post.setRequestHeader( "Content-type" , "text/xml; charset=GBK" );
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
System.out.println( "Response status code: " + result);
System.out.println( "Response body: " );
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();

推荐阅读
  • 引号快捷键_首选项和设置——自定义快捷键
    3.3自定义快捷键(CustomizingHotkeys)ChemDraw快捷键由一个XML文件定义,我们可以根据自己的需要, ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • centos安装Mysql的方法及步骤详解
    本文介绍了centos安装Mysql的两种方式:rpm方式和绿色方式安装,详细介绍了安装所需的软件包以及安装过程中的注意事项,包括检查是否安装成功的方法。通过本文,读者可以了解到在centos系统上如何正确安装Mysql。 ... [详细]
  • 浅解XXE与Portswigger Web Sec
    XXE与PortswiggerWebSec​相关链接:​博客园​安全脉搏​FreeBuf​XML的全称为XML外部实体注入,在学习的过程中发现有回显的XXE并不多,而 ... [详细]
  • KVC:Key-valuecodingisamechanismforindirectlyaccessinganobject’sattributesandrelations ... [详细]
  • 前言:原本纠结于Web模板,选了Handlebars。后来发现页面都是弱逻辑的,不支持复杂逻辑表达式。几乎要放弃之际,想起了Javascript中ev ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
author-avatar
810526猪肝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有