我正在使用嵌入Java的netty 4.1,并尝试从管道中的客户端POST请求中检索数据。我在网上找到了几种选择,但都没用。。。
也许有人对此有一个有用的想法。
向所有提供帮助的人致以问候和感谢。
管道:
p.addLast ("codec", new HttpServerCodec ());
p.addLast("decoder", new HttpRequestDecoder());
p.addLast("encoder", new HttpRequestEncoder());
p.addLast("handler",new InboundHandlerA());
处理程序:
private static class InboundHandlerA extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("Connected!");
ctx.fireChannelActive();
}
public void channelRead (ChannelHandlerContext channelHandlerCtxt, Object msg) throws Exception {
System.out.println(msg);
}
}
使用netty接收HTTP请求很简单,您可以使用以下管道执行此操作:
// Provides support for http objects:
p.addLast("codec", new HttpServerCodec());
// Deals with fragmentation in http traffic:
p.addLast("aggregator", new HttpObjectAggregator(Short.MAX_VALUE));
// Deals with optional compression of responses:
// p.addLast("aggregator", new HttpContentCompressor());
p.addLast("handler",new InboundHandlerA());
这可以与自定义的SimpleChannelInboundHandler一起使用
public class InboundHandlerA extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
public void channelActive(ChannelHandlerContext ctx) {
super.channelActive(ctx);
System.out.println("Connected!");
}
// Please keep in mind that this method
will be renamed to messageReceived(ChannelHandlerContext, I) in 5.0.
@Override
public void channelRead0 (ChannelHandlerContext ctx,
FullHttpRequest msg) throws Exception {
// Check for invalid http data:
if(msg.getDecoderResult() != DecoderResult.SUCCESS ) {
ctx.close();
return;
}
System.out.println("Recieved request!");
System.out.println("HTTP Method: " + msg.getMethod());
System.out.println("HTTP Version: " + msg.getProtocolVersion());
System.out.println("URI: " + msg.getUri());
System.out.println("Headers: " + msg.headers());
System.out.println("Trailing headers: " + msg.trailingHeaders());
ByteBuf data = msg.content();
System.out.println("POST/PUT length: " + data.readableBytes());
System.out.println("POST/PUT as string: ");
System.out.println("-- DATA --");
System.out.println(data.toString(StandardCharsets.UTF_8));
System.out.println("-- DATA END --");
// Send response back so the browser won't timeout
ByteBuf responseBytes = ctx.alloc().buffer();
responseBytes.writeBytes("Hello World".getBytes());
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBytes);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE,
"text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,
response.content().readableBytes());
response.headers().set(HttpHeaders.Names.CONNECTION,
HttpHeaders.Values.KEEP_ALIVE);
ctx.write(response);
}
}
上面的代码打印出了传入消息的所有细节,包括post数据。如果您只需要post数据,那么可以添加一个简单的If语句来过滤post响应类型
我正在使用POST请求向服务器发送CSV文件。 我正在使用一个类似文件的对象与 如果CSV文件相当大,并且我的内存有限,或者我使用类似文件的对象将永远不会在内存中加载整个文件,那么会有问题吗?我对此不确定。 我知道有流选项,但听起来更像是为了获得响应而不是发送数据。
<?php $requestBody = array( 'lang' => 'php', 'ver' => 'any' ); $http = HttpRequest::newSession(); $response = $http->post('http://www.baidu.com/', $requestBody);
原理 对于POST请求的处理,koa2没有封装获取参数的方法,需要通过解析上下文context中的原生node.js请求对象req,将POST表单数据解析成query string(例如:a=1&b=2&c=3),再将query string 解析成JSON格式(例如:{"a":"1", "b":"2", "c":"3"}) 注意:ctx.request是context经过封装的请求对象,ctx.
我有一个非常简单的HTML表单页面(它是src/main/resources/public/web.HTML中Spring Boot web应用程序的一部分),用于将一个字符串从文本区发布到Spring Boot web应用程序版本1.5.2。 和SpringBoot类来处理POST请求:
对传递的 URL 发出一个 POST 请求。 使用 XMLHttpRequest web api 对给定的url 发出一个 post 请求。 用 setRequestHeader 方法设置 HTTP 请求头的值。 通过调用给定的 callback 和 responseText 来处理 onload 事件。 通过运行提供的 err 函数,处理onerror事件。 省略第三个参数 data ,不发送数
问题内容: 我有以下django模板(将http:// IP / admin / start /分配给一个名为view的假设视图): 是视图中引用的Django模型的。每当单击“开始”提交输入时,我都希望“开始”视图在返回渲染页面之前使用函数中的数据。如何将POST(在本例中为隐藏输入)中发布的信息收集到Python变量中? 问题答案: 另外,你的隐藏字段还需要一个可靠的名称,然后是一个值: 然后