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;
        }
  • 相关阅读:
    组合,多态,封装
    继承and派生
    面向对象编程 类 后补充了元类 和单例
    数据结构与算法(Python)
    git版本控制系统命令
    python数据类型
    MVC与MTV模型及Django请求的生命周期
    linux目录文件及系统启动知识
    linux命令汇总
    Python字符串和列表的内置方法
  • 原文地址:https://www.cnblogs.com/jayGold/p/3325915.html
Copyright © 2011-2022 走看看