服务器之家

服务器之家 > 正文

java 中HttpClient传输xml字符串实例详解

时间:2020-09-13 12:20     来源/作者:Java之家

javaHttpClient传输xml字符串实例详解

介绍:我现在有一个对象page,需要将page对象转换为xml格式并以binary方式传输到服务端

其中涉及到的技术点有:

1、对象转xml流
2、输出流转输入流
3、httpClient发送二进制流数据

POM文件依赖配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部