zoukankan      html  css  js  c++  java
  • 如何发送post请求的时候传输xml文件?

    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; }
  • 相关阅读:
    python的dict和set
    python基础之dict和set
    python基础之条件判断和循环
    mongodb安装和配置,遇到问题和解决方法
    mybatis12--一级缓存
    mybatis11--多对多关联查询
    mybatis10--自连接多对一查询
    mybatis09--自连接一对多查询
    mybatis08--关联查询多对一
    mybatis07--关联查询一对多
  • 原文地址:https://www.cnblogs.com/zhaijing/p/7874035.html
Copyright © 2011-2022 走看看