http发送post请求
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); } }
http发送post请求的更多相关文章
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...
- Ajax发送POST请求SpringMVC页面跳转失败
问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- 在发送ajax请求时加时间戳或者随机数去除js缓存
在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...
- HttpUrlConnection发送url请求(后台springmvc)
1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...
- kattle 发送post请求
一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...
- 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?
file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- 【转载】JMeter学习(三十六)发送HTTPS请求
Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程中需要进行登录操作,而登录请求是HTTPS协议.这就需要对jmeter进行设置. ...
随机推荐
- swing菜单,常用组件,常用容器
1菜单 import javax.swing.*; import java.awt.*; import java.awt.event.InputEvent; import java.awt.event ...
- Python中的正斜杠与反斜杠
首先,"/"左倾斜是正斜杠,"\"右倾斜是反斜杠,可以记为:除号是正斜杠一般来说对于目录分隔符,Unix和Web用正斜杠/,Windows用反斜杠,但是现在Wi ...
- Nt内核函数原型and中文
NtLoadDriver 服务控制管理器加载设备驱动. NtUnloadDriver 服务控制管理器支持卸载指定的驱动程序. NtRegisterNewDevice 加载新驱动文件. NtQueryI ...
- WinForm------DateTime获取月第一天和最后一天取法
转载: http://blog.csdn.net/livening/article/details/6049341/ 代码: /// <summary> /// 取得某月的第一天 /// ...
- MD5 加密 代码
/*! 获取MD5 加密串 */ +(NSString *)getAuthorization:(NSString *)type Date:(NSDate *)date UserSecret:(NSSt ...
- 汇编下的i++与++i
故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时因为要去上课没给他说,正好今晚有空就测试了一下如下代码: 编译环境:VS2010 语言:C++ #include <iost ...
- 为ownCloud配置SSL连接
为ownCloud配置SSL连接 在你开始使用ownCloud之前,强烈建议你在ownCloud中启用SSL支持.使用SSL可以提供重要的安全好处,比如加密ownCloud流量并提供适当的验证.在本教 ...
- perl dbi 测试 mysql wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it ...
- sql Sever 修改表中的列名
EXEC SP_RENAME 'LeInterface.iYear','iLeYear','COLUMN' EXEC SP_RENAME 'LeInterface.imonth','iLemonth' ...
- Delphi default属性
Delphi default属性 Delphi控件指定属性的时候可以加上Default关键字,例如property Color default clBtnface.一直以为这个是构造类的时候的默认值, ...