Http Post 二进制通信
客户端请求和接收(使用了httpclient4.3 和netty3.5)
public static void httpPost11() {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(
"http://192.168.1.74:4321/v1/user/get_notice");
MultipartEntityBuilder d=MultipartEntityBuilder.create();
int a=10,b=11;
d.addBinaryBody("id", HighLowByteConvert.toHH(a));
d.addBinaryBody("ids", HighLowByteConvert.toHH(b));
try {
httpPost.setEntity(d.build());
CloseableHttpResponse httppHttpResponse2 = httpClient
.execute(httpPost);
if (httppHttpResponse2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
byte []c=EntityUtils.toByteArray(httppHttpResponse2.getEntity());
ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer(c);
System.out.println(channelBuffer.readInt());
System.out.println(channelBuffer.readInt());
}
else
{
}
httppHttpResponse2.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
服务端接收和发送
public void handle(HttpRequest request, Channel channel) throws Exception {
HttpMethod method = request.getMethod();
if(method.equals(HttpMethod.GET)){
handle(request.getUri(), channel);
}else if(method.equals(HttpMethod.POST)){
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request);
List< InterfaceHttpData> datas = decoder.getBodyHttpDatas();
for (InterfaceHttpData interfaceHttpData : datas) {
if(interfaceHttpData.getHttpDataType() == HttpDataType.Attribute)
{
Attribute attribute = (Attribute) interfaceHttpData;
System.out.println("id :" + attribute.getChannelBuffer().readInt());
}
}
sendResponse("", channel);
}
}
protected void sendResponse(String contextString, Channel channel){
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
channelBuffer.writeInt(1);
channelBuffer.writeInt(2);
response.setContent(channelBuffer);
response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); //application/octet-stream 也可以
channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
Http Post 二进制通信的更多相关文章
- 轻量级通信引擎StriveEngine —— C/S通信demo(2) —— 使用二进制协议 (附源码)
在网络上,交互的双方基于TCP或UDP进行通信,通信协议的格式通常分为两类:文本消息.二进制消息. 文本协议相对简单,通常使用一个特殊的标记符作为一个消息的结束. 二进制协议,通常是由消息头(Head ...
- C#轻量级通通讯组件StriveEngine —— C/S通信开源demo(2) —— 使用二进制协议 (附源码)
前段时间,有几个研究ESFramework通信框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送.不需要P2P.不存在好友关 ...
- ES6 二进制数组
二进制数组(ArrayBuffer对象.TypedArray视图和DataView视图)是JavaScript操作二进制数据的一个接口.这些对象早就存在,属于独立的规格(2011年2月发布),ES6将 ...
- web客户端与服务器端二进制传输
demo:https://pan.baidu.com/s/1gfEiJ1D 前端采用js,后端采用.net 主要实现js和服务器端之间用二进制通信
- es6二进制数组--基础
一.概念二进制数组由 ArrayBuffer对象 TypeArray 视图和DataView视图 三部分组成是javascript操作二进制数据的一个接口. 早在2011年2月就已经发布,但是由于ES ...
- REST 架构风格
目前基于网络应用的架构风格主要有三种: RPC架构风格 将服务器看作是由一些过程组成,客户端调用这些过程来执行特定的任务.SOAP就是RPC风格的一种架构.过程是动词性的(做某件事),因此RPC建 ...
- 实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现
[译]实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现 ken hu· 6 天前 原文(日文):ラピッドイテレーションを実現するゲームエンジンの設計 CEDEC2016上的一个 ...
- WebService的原理和过程
转自:http://blog.csdn.net/xiaoqiang081387/article/details/5694304 (一).XML WebService作用 XML WebService ...
- REST总结(转)
REST总结 REST(Representational State Transfer)是代表状态传输的缩写,它代表了分布式超媒体系统的体系结构风格,它是一种针对网络应用的设计和开 ...
随机推荐
- jump-game i&&ii 能否跳出区间 贪心
I: Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- 【DB2】设置表不记录日志,但是回滚导致表不可用报错:DB2 SQL Error: SQLCODE=-1477, SQLSTATE=55019, SQLERRMC=LEN.TMP;5;3, DRIVER=3.57.82
今天在生产发现个问题,很多表报错:SQLCODE=-1477 后来线下模拟出这种场景 出现这个问题,此时对表做reorg.runstats都是无效的. 那么怎么来解决这个问题呢? 只能重建表解决问题
- RHEL7 添加网卡并设置多个IP地址
首先在虚拟机管理界面中给该虚拟机添加一块虚拟网卡,VirtualBox虚拟机中要shutdown才能添加. [root@rhel7 ~]# nmcli device show | grep -i de ...
- java计算时间差, 日期差小结
转自:https://blog.csdn.net/sy793314598/article/details/79544796 1.java 7中的日历类Calendar Calendar类使用其静态的g ...
- [抄]OKR
OKR是Objective Key Result KPI是KeyPointIndicator OKR概览 OKR是一个目标管理工具.即目标与关键成果法,是一套明确和跟踪目标及其完成情况的管理工具和方法 ...
- Eclipse中10个最有用的快捷键组合(转)
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- java 获取当前日期和特殊日期格式转换
1.获取当前日期: package com.infomorrow.dao; import java.sql.Timestamp; import java.util.Calendar; import ...
- 【DeepLearning】Exercise:Convolution and Pooling
Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...
- Apache Rewrite规则详解[转]
1.Rewrite规则简介:Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式.如果要想用 ...