• 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;
        }
  • 相关阅读:
    如何完全卸载oracle和删除oracle在注册表中的注册信息
    win10 管理工具中添加 oracle 10g驱动
    failed to install tomcat6 service ,check your setting and permissions
    ORA-28000: the account is locked-的解决办法
    Intellij Idea乱码解决方案
    过三关 Java冒泡排序选择排序插入排序小练习
    Java坦克大战(四)
    Java坦克大战(三)
    Java坦克大战(二)
    Java坦克大战(一)
  • 原文地址:https://www.cnblogs.com/jayGold/p/3325915.html
Copyright © 2020-2023  润新知