栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java http xml请求

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java http xml请求

1 importjava.io.BufferedInputStream;2 importjava.io.BufferedReader;3 importjava.io.ByteArrayOutputStream;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.io.InputStreamReader;7 importjava.io.OutputStreamWriter;8 importjava.net.URI;9 importjava.net.URL;10 importjava.net.URLConnection;11

12 importorg.apache.commons.httpclient.HttpClient;13 importorg.apache.commons.httpclient.HttpStatus;14 importorg.apache.commons.httpclient.methods.PostMethod;15

16

20 public classTestMeetingInterface {21

22

25 public static voidmain(String[] args) {26

27 String url = "http://192.168.0.68/integration/xml";28 TestMeetingInterface tmi = newTestMeetingInterface();29 System.out.println(tmi.post(url,"listSummaryMeeting.xml"));30

31

49 }50

51

52

53

59 publicString post(String url,String xmlFileName){60 //关闭

61 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");62 System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");63 System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout");64

65 //创建httpclient工具对象

66 HttpClient client = newHttpClient();67 //创建post请求方法

68 PostMethod myPost = newPostMethod(url);69 //设置请求超时时间

70 client.setConnectionTimeout(300*1000);71 String responseString = null;72 try{73 //设置请求头部类型

74 myPost.setRequestHeader("Content-Type","text/xml");75 myPost.setRequestHeader("charset","utf-8");76

77 //设置请求体,即xml文本内容,注:这里写了两种方式,一种是直接获取xml内容字符串,一种是读取xml文件以流的形式78 //myPost.setRequestBody(xmlString);

79

80 InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName);81 myPost.setRequestBody(body);82 //myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));

83 int statusCode =client.executeMethod(myPost);84 if(statusCode ==HttpStatus.SC_OK){85 BufferedInputStream bis = newBufferedInputStream(myPost.getResponseBodyAsStream());86 byte[] bytes = new byte[1024];87 ByteArrayOutputStream bos = newByteArrayOutputStream();88 int count = 0;89 while((count = bis.read(bytes))!= -1){90 bos.write(bytes, 0, count);91 }92 byte[] strByte =bos.toByteArray();93 responseString = new String(strByte,0,strByte.length,"utf-8");94 bos.close();95 bis.close();96 }97 }catch(Exception e) {98 e.printStackTrace();99 }100 myPost.releaseConnection();101 returnresponseString;102 }103

104

108 public voidtestPost(String urlStr) {109 try{110 URL url = newURL(urlStr);111 URLConnection con =url.openConnection();112 con.setDoOutput(true);113 con.setRequestProperty("Pragma:", "no-cache");114 con.setRequestProperty("Cache-Control", "no-cache");115 con.setRequestProperty("Content-Type", "text/xml");116

117 OutputStreamWriter out = newOutputStreamWriter(con.getOutputStream());118 String xmlInfo =getXmlInfo();119 System.out.println("urlStr=" +urlStr);120 //System.out.println("xmlInfo=" + xmlInfo);

121 out.write(new String(xmlInfo.getBytes("UTF-8")));122 out.flush();123 out.close();124 BufferedReader br = new BufferedReader(newInputStreamReader(con125 .getInputStream()));126 String line = "";127 for (line = br.readLine(); line != null; line =br.readLine()) {128 System.out.println(line);129 }130 } catch(Exception e) {131 e.printStackTrace();132 }133 }134

135 privateString getXmlInfo() {136 StringBuilder sb = newStringBuilder();137 sb.append("");138 sb.append("");139 sb.append(" ");140 sb.append(" readMeetingStatus");141 sb.append(" meeting");142 sb.append(" xml");143 sb.append(" admin");144 sb.append(" admin");145 sb.append(" box");146 sb.append(" ");147 sb.append(" ");148 sb.append(" 43283344");149 sb.append(" ");150 sb.append("");151

152 returnsb.toString();153 }154 }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984980.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号