rt-thread中使用WebClient WebNet总结 http学习

HTTP学习资料
1. 需求背景
WebClient主要用来传输文件
WebNet用来支持cgi接口,需要支持get post put delete方式

2. webnet中使用
2.1 webnet存在问题
2.11 rt-thread 使用中option访问没有得到期望的响应,为了不改变底层组件库,自己在外部加了专门单独针对OPTION的响应

http 响应状态在rt-thread中只有200才是成功, 其它会报错误,要改下

/**
  * @brief  http option response
  * @param
  * @retval 0 成功   负数  失败
  */
void webnet_session_set_header_option(struct webnet_session *session, int code, const char* title)
{
    int length = 0;
    static const char* fmt = "HTTP/1.1 %d %s\r\n%s";
    
    static const char* Access = "Access-Control-Allow-Origin: *\r\n";        // 允许的站点
    static const char* Methods = "Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS\r\n"; // 允许的请求方式
    static const char* Headers = "Access-Control-Allow-Headers: Content-Type\r\n";   // 允许的类型
    static const char* Age = "Access-Control-Max-Age: 86400\r\n";       // 有效期  86400 一天类有效
    
    static const char* content = "Content-Type: %s\r\nContent-Length: %ld\r\nConnection: %s\r\n\r\n";
    static const char* content_nolength = "Content-Type: %s\r\nConnection: %s\r\n\r\n";

    char *ptr, *end_buffer;
    int offset;

    ptr = (char*)session->buffer;
    end_buffer = (char*)session->buffer + session->buffer_length;

    offset = rt_snprintf(ptr, end_buffer - ptr, fmt, code, title, WEBNET_SERVER);
    ptr += offset;
    
    gwai
    offset = rt_snprintf(ptr, end_buffer - ptr, Access);   
    ptr += offset;    

    offset = rt_snprintf(ptr, end_buffer - ptr, Methods);   
    ptr += offset;    

    offset = rt_snprintf(ptr, end_buffer - ptr, Headers);   
    ptr += offset;    

    offset = rt_snprintf(ptr, end_buffer - ptr, Age);   
    ptr += offset;
    
    offset = rt_snprintf(ptr, end_buffer - ptr, "Connection: Keep-Alive\r\n\r\n");
    ptr += offset;
    
    
    /* get the total length */
    length = ptr - (char*)session->buffer;

    /* invoke webnet event */
    if (webnet_module_handle_event(session, WEBNET_EVENT_RSP_HEADER) == WEBNET_MODULE_CONTINUE)
    {
        /* write to session */
        webnet_session_write(session, session->buffer, length);
    }
}

2.12 对于PUT DELTEE处理错误, 这里取巧了,懒的研究代码

            request->origin_method = request->method;     // 定义一个变量备份请求方式
            if( (WEBNET_DELETE == request->method) || (WEBNET_PUT == request->method) ){     
                request->method = WEBNET_POST;           // 将 PUT DELETE请求方式改为POST方式处理
            }

在wn_module_cgi.c文件中修改
修改

3. webclient 使用
webclient 客服端用于文件传输,采用http multipart/form-data单提交方式,由于http基于tcp,tcp基于窗口确认机制其速度远远高于TFTP的停止等待机制,特别是对于远程传输文件。

3.1 GET获取文件可以直接用不用修改
3.2 POST提交文件,由于需要附件信息,因此需要修改

postman中配置
在这里插入图片描述
对应的http协议标准格式

POST / HTTP/1.1
Host: e944r8404.oicp.vip
Content-Length: 305
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW    #一定要有\r\n换行

----WebKitFormBoundary7MA4YWxkTrZu0gW       # boundary 分割符
Content-Disposition: form-data; name=""; filename="/C:/Users/admin/Desktop/222222222222222/tmp/t1.txt"
Content-Type: text/plain

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW         # boundary 分割符
Content-Disposition: form-data; name="path"   # 表单描述
                                              # \r\n
/resource                                     # 内容  
----WebKitFormBoundary7MA4YWxkTrZu0gW--       # 最后一个boundary 有-- 

wireshark抓包, 本次传输两个text表单,两文件表单
在这里插入图片描述

4. 关于http 协议

4.1 断点续传参考资料
4.2 http 常规头数据帧格式
whireshak抓包
在这里插入图片描述
所有头信息以\r\n分割,头与消息实体也是\r\n分割

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,您可以在 Spring Cloud Gateway 使用 WebClient 进行服务调用。WebClient 是 Spring WebFlux 的一个非阻塞式 HTTP 客户端,可以用于调用其他服务的 RESTful API。 下面是一个简单的示例,展示了如何在 Spring Cloud Gateway 使用 WebClient 进行服务调用: 1. 首先,在您的 Spring Cloud Gateway 项目添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` 2. 然后,在您的 Spring Cloud Gateway 配置类,注入一个 WebClient 对象: ``` @Configuration public class GatewayConfig { @Bean public WebClient webClient() { return WebClient.builder().build(); } } ``` 3. 最后,在您的路由配置使用注入的 WebClient 对象进行服务调用: ``` @Configuration public class GatewayRoutesConfig { @Autowired private WebClient webClient; @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("example", r -> r.path("/example") .uri("http://example.com")) .route("example-api", r -> r.path("/example-api/**") .filters(f -> f.rewritePath("/example-api/(?<path>.*)", "/${path}")) .uri("http://example.com")) .route("example-service", r -> r.path("/example-service/**") .filters(f -> f.rewritePath("/example-service/(?<path>.*)", "/${path}")) .uri("lb://example-service")) .route("example-service-webclient", r -> r.path("/example-service-webclient/**") .uri("http://example-service.com") .filter((exchange, chain) -> { URI uri = exchange.getRequest().getURI(); String path = uri.getPath().replace("/example-service-webclient", ""); return webClient .method(exchange.getRequest().getMethod()) .uri("http://example-service.com" + path) .headers(headers -> headers.addAll(exchange.getRequest().getHeaders())) .body(exchange.getRequest().getBody()) .exchange() .flatMap(clientResponse -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().putAll(clientResponse.headers().asHttpHeaders()); response.setStatusCode(clientResponse.statusCode()); return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())); }); })) .build(); } } ``` 在上面的示例,我们注入了一个 WebClient 对象,并在路由配置使用它进行服务调用。在 `example-service-webclient` 路由,我们使用 `webClient` 对象发出了一个 HTTP 请求,并将响应写回到响应流。需要注意的是,我们需要将请求的头部和请求体等信息都传递给 WebClient,以确保请求可以正确地被发送。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值