博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
http发送post请求
阅读量:7069 次
发布时间:2019-06-28

本文共 7543 字,大约阅读时间需要 25 分钟。

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字符串
*/ 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); } }

 

转载地址:http://syhll.baihongyu.com/

你可能感兴趣的文章
Zookeeper+Kafka完全分布式实战部署
查看>>
python中写日志log
查看>>
洛谷P2327 [SCOI2005]扫雷 枚举 搜索
查看>>
实现HTTP文件下载
查看>>
[linux,springboot] - 记一次java项目部署
查看>>
Android笔记之——线程
查看>>
Dedecms channel 标签 指定typeid时 currentstyle无效的修复办法
查看>>
[Go] 并发和并行的区别
查看>>
如何学习python
查看>>
Ta-Lib环境搭建及代码分析
查看>>
.NET Framework 中的设计模式
查看>>
JavaScript window对象
查看>>
JS如何遍历Object中的所有属性?
查看>>
LINUX VI 常用命令
查看>>
跨域iframe的高度自适应
查看>>
单相、二相、三相区别
查看>>
RabbitMQ要点
查看>>
Hello jna
查看>>
leetcode------Spiral Matrix II
查看>>
设置Session超时
查看>>