zoukankan      html  css  js  c++  java
  • File 参数 模拟POST请求

        public String OperationOrderSend(Map<String,String> params,String apiMethodName)
            throws IOException {
                Map<String,String> textParams=new HashMap<String,String>(params); //文本请求参数 
                Map<String,String> fileParams=new HashMap<String,String>(); //文件请求参数
                if(apiMethodName.equals("sendGoods")){
                    fileParams.put("sendGoods",params.get("sendGoods"));
                    textParams.remove("sendGoods");
                }
                textParams.put("gShopID", shopId);
                String validate = getValidateString(apiMethodName, textParams, apiKey);
                System.out.println("是这个验证码么:"+validate);
                
                String targetURL = "http://api.dangdang.com/v2/sendGoods.php"; // servleturl
                HttpClient httpClient = new HttpClient();
                PostMethod postMethod = new PostMethod(targetURL);
                
                
                
                String temp = null;
                temp = ServletActionContext.getServletContext().getRealPath("");
                temp = temp+"//temp//temp.xml";
                
                File file = new File(temp);
                Part [] parts = new Part[3];
                parts[0] = new StringPart("gShopID",shopId);
                try {
                    parts[1] = new FilePart("sendGoods", file,"text/xml","GBK");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }//第三个参数就是说明这个文件类型是xml的,不用这个的话,接口会返回说上传的文件不是xml类型
                parts[2] = new StringPart("validateString",getValidateString(apiMethodName, textParams, apiKey)); 
                postMethod.setRequestEntity(new    MultipartRequestEntity(parts,postMethod.getParams())); 
                int statusCode = httpClient.executeMethod(postMethod);
                System.out.println("反馈信息为:"+new String(postMethod.getResponseBody()));;
                String info = new String(postMethod.getResponseBody());
                postMethod.releaseConnection();
                return info;
        }
  • 相关阅读:
    OpenStack 发行版本
    刷新linux硬盘存储接口
    LVM实践
    LVM man帮助
    ansible --help 文档
    nmcli connection modify eth1 ipv4.addr "192.168.31.23" ipv4.method manual
    自己动手使用, MetaWeblog 发布博客(cnblogs)
    测试图片上传 on Markdown editor
    大批量更新数据mysql批量更新的四种方法
    PHP print_r 转换/还原为数组
  • 原文地址:https://www.cnblogs.com/jayGold/p/3325915.html
Copyright © 2011-2022 走看看