Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

发起http请求报错,tbox 1.7.5 报错 #264

Closed
edidada opened this issue Aug 30, 2024 · 8 comments
Closed

发起http请求报错,tbox 1.7.5 报错 #264

edidada opened this issue Aug 30, 2024 · 8 comments

Comments

@edidada
Copy link

edidada commented Aug 30, 2024

#include "tbox/tbox.h"

int main(int argc, char** argv)
{
  // 初始化Tbox库
  if (!tb_init(tb_null, tb_null))
  {
    return -1;
  }

  // 执行HTTP请求
  tb_http_ref_t http = tb_http_init();
  if (http)
  {
    // 设置 HTTP 方法为 POST
    tb_http_ctrl(http, TB_HTTP_OPTION_SET_METHOD, TB_HTTP_METHOD_POST);

    // 设置 URL
    tb_http_ctrl(http, TB_HTTP_OPTION_SET_URL, "http://101.43.12.32:8090/user/list");

    // 打开HTTP请求
    if (tb_http_open(http))
    {
      // 读取响应数据
      tb_size_t read_size = 0;
      tb_byte_t buffer[8192];
      while ((read_size = tb_http_read(http, buffer, sizeof(buffer))) > 0)
      {
        // 打印响应内容
        tb_printf("%.*s", (int)read_size, buffer);
      }

      // 关闭HTTP请求
      tb_http_close(http);
    }
    else
    {
      tb_printf("Failed to open HTTP request.\n");
    }

    // 释放HTTP资源
    tb_http_exit(http);
  }
  else
  {
    tb_printf("Failed to initialize HTTP.\n");
  }

  // 释放Tbox库
  tb_exit();
  return 0;
}
if (tb_http_open(http))

这一行报错,http.c 972行 tb_assert_and_check_return_val(http->bopened, -1);
这个报错,怎么修改代码?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: Initiate an http request and report an error, tbox 1.7.5 reports an error

@waruqi
Copy link
Member

waruqi commented Aug 30, 2024

你那是 read 报错,这个是非阻塞接口 要结合 wait 来用。参考 bread 的实现。。或者直接用 bread

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Your read error is reported, this is a non-blocking interface and should be used in combination with wait. See the implementation of bread. . Or just use bread

@edidada
Copy link
Author

edidada commented Aug 31, 2024

#include "tbox/tbox.h"

int main(int argc, char** argv)
{
  // 初始化Tbox库
  if (!tb_init(tb_null, tb_null))
  {
    return -1;
  }

  // 执行HTTP请求
  tb_http_ref_t http = tb_http_init();
  if (http)
  {
    // 设置 HTTP 方法为 POST
    tb_http_ctrl(http, TB_HTTP_OPTION_SET_METHOD, TB_HTTP_METHOD_POST);

    // 设置 URL
    tb_http_ctrl(http, TB_HTTP_OPTION_SET_URL, "http://101.43.12.32:8090/user/list");

    // 打开HTTP请求
    if (tb_http_open(http))
    {
      
    }
    else
    {
      tb_printf("Failed to open HTTP request.\n");
    }

    // 释放HTTP资源
    tb_http_exit(http);
  }
  else
  {
    tb_printf("Failed to initialize HTTP.\n");
  }

  // 释放Tbox库
  tb_exit();
  return 0;
}

Failed to initialize HTTP.\ 跟read/bread函数没有关系

@edidada
Copy link
Author

edidada commented Aug 31, 2024

#include "tbox/tbox.h"

int main(int argc, char** argv)
{
    // 初始化Tbox库
    if (!tb_init(tb_null, tb_null))
    {
      return -1;
    }
    // 执行HTTP请求
    tb_http_ref_t http = tb_http_init();
    if (http)
    {
        tb_http_ctrl(http, TB_HTTP_OPTION_SET_METHOD, TB_HTTP_METHOD_GET);
        tb_http_ctrl(http, TB_HTTP_OPTION_SET_URL, "http://101.43.12.32:8090/user/list");
        tb_http_open(http);
        // 读取响应数据
        tb_size_t read_size = 0;
        tb_byte_t buffer[8192];
        while ((read_size = tb_http_bread(http, buffer, sizeof(buffer))) > 0)
        {
            // 打印响应内容
            tb_trace_i("%.*s", (int)read_size, buffer);
        }

        // 关闭HTTP请求
        tb_http_close(http);
    }

    // 释放HTTP资源
    tb_http_exit(http);

    // 释放Tbox库
    tb_exit();
    return 0;
}

报错:
[tbox]: [http]: [assert]: expr[http->bopened] at tb_http_bread(): 984, src/tbox/network/http.c at tb_assert_impl(): 300, src/tbox/network/../prefix/assert.h
[tbox]: [http]: [error]: abort at tb_assert_impl(): 309, src/tbox/network/../prefix/assert.h

@waruqi
Copy link
Member

waruqi commented Aug 31, 2024

这一行报错,http.c 972行 tb_assert_and_check_return_val(http->bopened, -1);
这个报错,怎么修改代码?

Failed to initialize HTTP.\ 跟read/bread函数没有关系

你都跑进 read 报错了。。除非你 open 没判断返回,失败后,继续调用 read , 但是跟你一开始贴的代码完全对不上。。凡事贴错误,贴代码 得对上。

tb_assert_and_check_return_val(http->bopened, -1);

报错:
[tbox]: [http]: [assert]: expr[http->bopened] at tb_http_bread(): 984, src/tbox/network/http.c at tb_assert_impl(): 300, src/tbox/network/../prefix/assert.h
[tbox]: [http]: [error]: abort at tb_assert_impl(): 309, src/tbox/network/../prefix/assert.h

这里报 read error 是你 open 没判断返回值。。

tb_http_open(http);
// 读取响应数据
tb_size_t read_size = 0;
tb_byte_t buffer[8192];
while ((read_size = tb_http_bread(http, buffer, sizeof(buffer))) > 0)

至于 open 为什么失败,自己调下你们的服务器,看下为什么会无法连接

@waruqi
Copy link
Member

waruqi commented Aug 31, 2024

你自己翻下 demo 代码,和实现源码,自己调下。。tbox 我没空维护。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


You can download the demo code and implementation source code yourself and adjust them yourself. . I don't have time to maintain tbox.

@edidada edidada closed this as completed Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants