package com.j1.soa.resource.member.oracle.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

import com.j1.soa.common.Md5Util;

public class HttpText {

     final static String url = "http://192.168.200.53:8081/httpServer/c_i/common_i";
    //final static String url = "http://127.0.0.1:8080/httpServer/c_i/common_i";

    /**
     * 发送HttpPost请求
     * @param strURL  服务地址
     * @param sign  加密后的32 大写 MD5
     * @return 成功:返回json字符串<br/>
     */
    public static String post(String strURL,  String format,String method ,String sessionKey,String up_Date,String version,String sign) {
        System.out.println(strURL);
        System.out.println(sign);
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
             connection.setDoOutput(true);
             connection.setDoInput(true);
             connection.setRequestMethod("POST");
             connection.setUseCaches(false);
             connection.setInstanceFollowRedirects(true);
           //  connection.setContentType("text/xml;charset=utf-8");
             connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
             connection.setRequestProperty("ContentType", "utf-8"); // 设置发送数据的格式
             connection.setRequestProperty("Accept-Charset", "utf-8");
             connection.connect();
               //POST请求
          //   BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
             PrintWriter out =   new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); // utf-8编码  ;
            JSONObject obj = new JSONObject();
            obj.element("sessionKey", sessionKey);
            obj.element("method",method);
            obj.element("format", format);
            obj.element("up_Date", up_Date);
            obj.element("sign", sign);
            obj.element("version", version);
           JSONObject objbusinessdate = new JSONObject();
           objbusinessdate.element("memCardNo", "f999999299-ffJ1");//卡号
            objbusinessdate.element("contactor", "移动人");//姓名
             objbusinessdate.element("idCardCode", "32080220163212012");//身份证
           objbusinessdate.element("sex", "F");//性别
           objbusinessdate.element("birthDay", "2016-03-21");//生日 YYYY-MM-DD
        //    objbusinessdate.element("age",  19);//年龄
             objbusinessdate.element("address", "移动电话");// 地址
        //    objbusinessdate.element("hTel", "1111111111");// 座机
             objbusinessdate.element("mTel", "00800780");// 移动电话
           objbusinessdate.element("eMail", "11111@j1.com");//email
           objbusinessdate.element("enrDeptId", "1111");//部门id
             objbusinessdate.element("roptrId", "3219");//操作人id
            objbusinessdate.element("ybCardCode", "111111111111");//医保卡
         //   objbusinessdate.element("qqCode", "111111");//qq
            objbusinessdate.element("note", "移动电话"); //备注
            //"contactor":"15821023367","sex":"1","mTel":"15821023367","eMail":"fufu_pupu@163.com","regTime":"2016-11-22 10:18:19"
           /*** JSONObject objbusinessdate = new JSONObject();
                       objbusinessdate.element("username", "tyf");//  username ,
                       objbusinessdate.element("password", "1111"); //password

                       /***
                        * select    te.tranid     tranId,
                                    ca.contactor  contactor ,
                                    ca.memcardno  memCardNo,
                                     sa.retnamt   discount,
                                    sa.saleamt     saleAmt ,
                                    sa.trandate    tranDate,
                                    cae.articode   artiCode,
                                    cae.artiname   artiName,
                                    -te.artiqty    artiQty ,
                                    te.saleprice   salePrice

                 JSONObject objbusinessdate = new JSONObject();
                        objbusinessdate.element("deptNo", "000");//门店编码
                        objbusinessdate.element("deptName", "000");//门店名称
                        objbusinessdate.element("tranid", "1123");//  销售订单号 ,
                        objbusinessdate.element("contactor", "000");// 会员名称
                        objbusinessdate.element("memCardNo", "000");//会员卡号
                        objbusinessdate.element("discount", "000");//优惠
                        objbusinessdate.element("mTel", "000");//手机号码
                JSONObject artiitem = new JSONObject();         

                        artiitem.element("saleAmt", "000");//实付金额
                        artiitem.element("tranDate", "000");//销售时间
                        artiitem.element("artiCode", "000");      //商品编码
                        artiitem.element("artiName", "000");//商品名称
                        artiitem.element("artiQty", "000");//数量
                        artiitem.element("salePrice", "000");  //成交价格
                 objbusinessdate.element("artiitem", artiitem);
                        */
          //  String txt = new String(objbusinessdate.toString().getBytes(), "utf-8");
            obj.element("businessData", objbusinessdate);

            System.out.println(obj.toString());

           // out.write(obj.toString().getBytes());

            // 发送请求参数
            out.print(obj.toString() );
            // flush输出流的缓冲
            out.flush();

            // 读取响应
            int length = (int) connection.getContentLength();// 获取长度
            System.out.println("length:"+length);
            BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while( (line = rd.readLine())!=null){
                buffer.append(line);
            }
            out.close();
            rd.close(); 

            connection.disconnect();
            System.out.println(buffer.toString()+"  0000");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "error"; // 自定义错误信息
    }

    public static void main(String[] args) {

        //MD5加密顺序  format  meThod  sessionKey token  up_date  version
        //           format+method+sessionKey+token+up_date+version
         String token="91A1643059824847938125BA0AC0F557";//"bf0d8da2736e4d8caea055d863610a33  ";91A1643059824847938125BA0AC0F557 //token 不产于传送 但是参与加密

         String format ="json";      //传送方式
         String method ="saveMember";//"queryTbl_Employee " erpTranDetailCount_ToJ1 saveMember ;//调用方法
         String sessionKey ="123456789078945";//sessionkey
         String up_date ="2016-10-27";//上传日期  yyyy-mm-dd
         String version ="1.0.2";//版本号

     //  TranIdListServiceImpl.testhttprequest();
         //System.exit(0);
         String sign=null;
        try {
            sign = Md5Util.Bit32(format+method+sessionKey +token+up_date+version);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          post(url,format,method ,sessionKey,up_date,version,sign);
    }

}

最新文章

  1. Linux账号密码过期会导致crontab作业不能执行
  2. [Cocos2D-x For WP8]Sprite精灵
  3. Linux 有问必答:如何知道进程运行在哪个 CPU 内核上?
  4. linux下分卷tar.bz文件的合并并解压缩
  5. 创建Mysql 序列
  6. jquery页面无刷新切换皮肤并保存
  7. Redis的安装、配置 --转载
  8. php笔记03:布尔类型,字符串,浮点数
  9. 编写Qt Designer自定义控件(一)——如何创建并使用Qt自定义控件
  10. linux安装composer
  11. stagefright框架(七)-Audio和Video的同步
  12. git常用命令记录
  13. JS操作数组常用的方法
  14. [读书笔记] 三、搭建基于Spring boot的JavaWeb项目
  15. WinServer2012 R2忘记密码的解决方案+远程连接另一种莫名其妙故障
  16. memset库函数
  17. 【费马小定理】BZOJ3260 跳
  18. PHP 消息队列 详解
  19. item 1:理解template类型的推导
  20. jenkins发版脚本更新

热门文章

  1. MySQL命令mysqldump参数大全
  2. WPFPath素材
  3. (转)C#.NET使用TTS引擎实现文语转换
  4. Dom4j修改xml文档
  5. C语言刷新缓冲区(转载)
  6. Lua----注意事项
  7. New ipad install Metasploit(New ipad 安装Metasploit)
  8. java8-concurrency-tutorial-thread-executor-examples
  9. Codeforces 23E Tree
  10. HDU_2022——海选女主角