博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何发送post请求的时候传输xml文件?
阅读量:7231 次
发布时间:2019-06-29

本文共 1687 字,大约阅读时间需要 5 分钟。

import java.io.*; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
 
public static String requestPost(String url,String xmlFileName) {        HttpClient client = new HttpClient();        PostMethod myPost=new PostMethod(url);        String responseString=null;        try{
//将文件内容写入变量 BufferedReader bfr=null; StringBuffer sb=new StringBuffer(); bfr=new BufferedReader(new FileReader(new File(xmlFileName))); String line=null; while ((line=bfr.readLine())!=null){ sb.append(line); } myPost.setRequestEntity(new StringRequestEntity(sb.toString(),"text/xml","utf-8")); int statusCode = client.executeMethod(myPost); if(statusCode == HttpStatus.SC_OK) { BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream()); byte[] bytes = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int count = 0; while ((count = bis.read(bytes)) != -1) { bos.write(bytes, 0, count); } byte[] strByte = bos.toByteArray(); responseString = new String(strByte, 0, strByte.length, "utf-8"); bos.close(); bis.close(); } }catch (Exception e){ e.printStackTrace(); } myPost.releaseConnection(); return responseString; }

 

转载于:https://www.cnblogs.com/zhaijing/p/7874035.html

你可能感兴趣的文章
flask 中从eml中获取头邮件头信息
查看>>
laravel上传文件&获取请求实例(隐式)
查看>>
适配低内存手机让app存在多个进程
查看>>
BW中DSO的分类
查看>>
echo -en
查看>>
Mysql 复制(Replication)实现
查看>>
我的友情链接
查看>>
jar生成exe可执行的程序
查看>>
date 转化为 指定格式的String
查看>>
使用virtualbox安装RHEL 6.2+Oracle 11g
查看>>
系统的域名服务
查看>>
MySQL 复制表结构
查看>>
《Effective C#》条款8:确保0为值类型的有效状态
查看>>
动态迁移应用服务器(Esxi 动态迁移技术,业务不间断,在线迁移)
查看>>
systemd coding style
查看>>
warning: control reaches end of non-void function
查看>>
Tkinter, a Gui for python
查看>>
android开发之webservice介绍
查看>>
纯js页面跳转整理
查看>>
目标:嗯,每天进步一点点,每周坚持写一点
查看>>