java 中HttpClient傳輸xml字符串實(shí)例詳解
java 中HttpClient傳輸xml字符串實(shí)例詳解
介紹:我現(xiàn)在有一個(gè)對(duì)象page,需要將page對(duì)象轉(zhuǎn)換為xml格式并以binary方式傳輸?shù)椒?wù)端
其中涉及到的技術(shù)點(diǎn)有:
1、對(duì)象轉(zhuǎn)xml流
2、輸出流轉(zhuǎn)輸入流
3、httpClient發(fā)送二進(jìn)制流數(shù)據(jù)
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{
//定義對(duì)象
Page page =new Page();
page.setTitle("testPage");
page.setSyntax("xwiki/2.0");
page.setContent("This is a testPage");
page.setId("xwiki:Main.testPage");
//初始化并轉(zhuǎn)換對(duì)象為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 );
//將流轉(zhuǎn)換并放入到InputStreamEntity中
InputStreamEntity inputStreamEntity=new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()));
//發(fā)送請(qǐng)求
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();
//獲取返回結(jié)果
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));
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求
- java爬蟲之使用HttpClient模擬瀏覽器發(fā)送請(qǐng)求方法詳解
- java httpclient設(shè)置超時(shí)時(shí)間和代理的方法
- 如何使用HttpClient發(fā)送java對(duì)象到服務(wù)器
- Java11新特性之HttpClient小試牛刀
- java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例
- JAVA通過HttpClient發(fā)送HTTP請(qǐng)求的方法示例
- JAVA利用HttpClient進(jìn)行HTTPS接口調(diào)用的方法
- java web中 HttpClient模擬瀏覽器登錄后發(fā)起請(qǐng)求
- Java爬蟲Jsoup+httpclient獲取動(dòng)態(tài)生成的數(shù)據(jù)
- Java httpClient介紹以及使用示例
相關(guān)文章
Java實(shí)現(xiàn)將枚舉類轉(zhuǎn)為json并返回給前端
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)將枚舉類轉(zhuǎn)為json并返回給前端的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
詳解spring mvc對(duì)異步請(qǐng)求的處理
spring mvc3.2及以上版本增加了對(duì)請(qǐng)求的異步處理,是在servlet3的基礎(chǔ)上進(jìn)行封裝的,有興趣的可以了解一下。2017-01-01
TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了TF-IDF理解及其Java實(shí)現(xiàn)代碼實(shí)例,簡單介紹了tfidf算法及其相應(yīng)公式,然后分享了Java實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例
這篇文章主要介紹了Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Cloud,感興趣的朋友可以了解下2021-03-03
更簡單更高效的Mybatis?Plus最新代碼生成器AutoGenerator
這篇文章主要為大家介紹了更簡單更高效的Mybatis?Plus最新代碼生成器AutoGenerator使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Java網(wǎng)絡(luò)編程基礎(chǔ)篇之單向通信
這篇文章主要介紹了Java網(wǎng)絡(luò)編程里通過套接字實(shí)現(xiàn)單向通信的方法及相關(guān)實(shí)例,屬于網(wǎng)絡(luò)編程入門程序,雖然簡單,但具有一定參考價(jià)值,需要的朋友可以參考下。2017-09-09

