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进行设置. ...
随机推荐
- jquery的几种异步请求,ajax
http://blog.csdn.net/a5489888/article/details/8523316
- 策略模式Strategy(对象行为型)
原文地址:http://blog.csdn.net/hguisu/article/details/75582491.策略模式:定义一系列的算法,把每一个算法封装起来, 并且使它们可相互替换.本模式使得 ...
- 使用CAEmitterLayer产生粒子效果
ViewController.m #import "ViewController.h" @implementation ViewController - (void)viewDid ...
- 接入淘宝SDK(OneSDK)和支付宝SDK(AlipaySDK)出现 duplicate symbols for architecture i386
起初我在我的项目中先接入了AlipaySDK,没有出现什么问题,之后想要接入淘宝SDK之后,就出现了duplicate symbols for architecture i386的错误 经过一段时间排 ...
- Helloworld和程序员人生
转:Helloworld和程序员人生 高中时期 10 PRINT "HELLO WORLD" 20 END 大学新生 program Hello(input, output) be ...
- [转]C++堆和栈的区别
一.预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...
- Linux入门 (笔记)
Man can conquer nature. "人定胜天" 一.基本操作 1.重要快捷键 Tab 补全命令.目录.参数.文件名等 Ctrl+c 强制终止当前的程序 Ctrl+ ...
- 使用 HTML5 设计辅助功能
使用 HTML5 设计辅助功能 Rajesh Lal 下载代码示例 如果您真的对面向广大受众感兴趣,将需要为网站设计辅助功能. 辅助功能使网页更易于访问.更易于使用,可供每个人浏览. 通常,使用最新的 ...
- php代码的一些高效写法
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...
- 你不知的IE的bug及其解决方案
E令人咬牙切齿的bug不胜枚举,其中IE6更是臭名昭著,令人发指.这里总结出IE下最为严重的5个bug,及其应对方案. 1.IE6下无法显示png格式的透明信息 这个bug是众多网页设计师的噩梦,虽然 ...