• 如何发送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; }
  • 相关阅读:
    Linux-redis实现session复制
    Linux-sentinel实现redis主从高可用
    Linux-初识redis
    Docker-docker-machine
    Linux-Nginx服务调优(下)
    Linux-Nginx服务调优(上)
    Linux-nginx.conf配置文件模板
    Linux-编译安装Nginx
    VS Code (visual studio code) VSC 编辑器(微软出品,js开发的编辑器)
    好用的在线工具
  • 原文地址:https://www.cnblogs.com/zhaijing/p/7874035.html
Copyright © 2020-2023  润新知