java 中HttpClient传输xml字符串实例详解
作者:lqh 时间:2023-11-24 13:40:54
java 中HttpClient传输xml字符串实例详解
介绍:我现在有一个对象page,需要将page对象转换为xml格式并以binary方式传输到服务端
其中涉及到的技术点有:
1、对象转xml流
2、输出流转输入流
3、httpClient发送二进制流数据
POM文件依赖配置
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-rest-model</artifactId>
<version>7.2</version>
</dependency>
</dependencies>
java代码示例
public void testNewPage() throws Exception{
//定义对象
Page page =new Page();
page.setTitle("testPage");
page.setSyntax("xwiki/2.0");
page.setContent("This is a testPage");
page.setId("xwiki:Main.testPage");
//初始化并转换对象为xml文件的流
JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
Marshaller marshaller=context.createMarshaller();
Unmarshaller unmarshaller = context.createUnmarshaller();
ByteArrayOutputStream out = new ByteArrayOutputStream();
marshaller.marshal( page, out );
//将流转换并放入到InputStreamEntity中
InputStreamEntity inputStreamEntity=new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()));
//发送请求
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpUriRequest httpPost = RequestBuilder.put()
.setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/testPage"))
.setEntity(inputStreamEntity)
.setHeader("Content-Type", "application/xml")
.setHeader("Cookie", cookieStr).build();
//获取返回结果
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity responseEntity=response.getEntity();
System.out.println(responseEntity);
if(response.getStatusLine().getStatusCode()<400){
Page responsePage = (Page) unmarshaller.unmarshal(responseEntity.getContent());
System.out.println(responsePage);
// System.out.println(new Gson().toJson(responsePage));
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/gsying1474/article/details/51505714
标签:HttpClient,xml
0
投稿
猜你喜欢
官方详解HDFS Balancer工具主要调优参数
2023-07-24 04:43:47
详解Spring-bean的循环依赖以及解决方式
2023-08-18 18:30:38
浅入浅出的讲解Spring循环依赖问题
2023-11-03 07:16:11
Android自定义ProgressDialog加载图片
2022-07-28 22:08:39
java8 stream自定义分组求和并排序的实现
2022-09-12 04:08:26
Android编程简单实现九宫格示例
2021-12-06 21:43:57
Spring Boot2中如何优雅地个性化定制Jackson实现示例
2021-09-27 12:21:15
Unity实现简单虚拟摇杆
2023-08-04 17:33:09
详解Java虚拟机管理的内存运行时数据区域
2023-12-04 01:32:35
Java中使用Lambda表达式和函数编程示例
2022-05-06 03:30:40
Android编程实现左右滑动切换背景的方法
2022-03-21 17:56:49
Android启动优化之延时加载的步骤详解
2023-07-10 20:43:41
JAVAsynchronized原理详解
2023-05-17 12:18:32
简单了解Spring beanfactory循环依赖命名重复属性
2023-10-27 19:39:14
浅谈Java HttpURLConnection请求方式
2021-06-24 12:34:49
springboot多模块化整合mybatis,mapper自动注入失败问题及解决
2022-02-20 08:06:29
android同时控制EditText输入字符个数和禁止特殊字符输入的方法
2021-08-01 14:16:46
基于C#调用c++Dll结构体数组指针的问题详解
2021-12-10 23:16:41
详解C# 泛型中的数据类型判定与转换
2023-05-03 08:08:36
java加密算法分享(rsa解密、对称加密、md5加密)
2021-08-30 16:22:08