| 大家好,我是来自大陆北方的网友,相信大家手里都拿到了Ai-M61-32U 有些人苦恼不知道怎么连接wifi和发送http请求。
 默认请求成功
 换了一个网站 直接卡死
 又换了一个网站
 
 查看代码复制代码2023-12-22 22:43:07.600] → ␛[0m[I][] CODE_WIFI_ON_CONNECTED␍␊
[2023-12-22 22:43:07.724] → ␛[0m[I][] [APP] [EVT] wifi_event_handler, CODE_WIFI_ON_GOT_IP␍␊
[0] wl1: MAC=b4:0e:cf:2b:09:e3 ip=192.168.43.168/24 UP,CONNECTED␊
␛[0m[I][] CODE_WIFI_ON_GOT_IP␍␊
Http client task start ...␍␊
Host:ip.geo.iqiyi.com/cityjson, Server ip Address : 255.255.255.255:80␍␊
[2023-12-22 22:43:26.183] → Http client connect server falied!␍␊
exception_entry␍␊
mcause=38000001␍␊
mepc:00000000␍␊
mtval:00000000␍␊
Instruction access fault␍␊
http路径未处理 只能请求主页。
 定位问题:
 wifi_http_client.h
 
 启动ai分析
 
 get_buf是一个默认的HTTP请求指令复制代码导入所需的头文件和库:代码中使用了一些系统和网络相关的头文件和库,包括unistd.h、stdlib.h、stdio.h、sys/socket.h、lwip/api.h、lwip/arch.h、lwip/opt.h、lwip/inet.h、lwip/errno.h和netdb.h。
定义宏和全局变量:代码中定义了一些宏和全局变量,包括HOST_NAME、get_buf和recv_buf。其中,HOST_NAME是一个未使用的宏,get_buf是一个默认的HTTP请求指令,recv_buf是接收缓冲区。
定义信号处理函数:代码中定义了一个名为test_close的信号处理函数,用于在收到信号时关闭socket连接。
定义默认请求和帮助信息:代码中定义了一个名为PING_USAGE的宏,用于显示默认请求的使用方法和参数说明。
定义HTTP客户端初始化函数:代码中定义了一个名为wifi_test_http_client_init的函数,用于初始化HTTP客户端并发送HTTP请求。该函数接受命令行参数作为输入,包括hostname和port。
解析命令行参数:函数中解析命令行参数,获取hostname和port。
创建socket连接:函数中使用socket函数创建一个TCP socket连接。
连接服务器:函数中使用connect函数将socket连接到指定的服务器。
发送HTTP请求:函数中使用write函数向服务器发送HTTP请求。
接收和打印响应:函数中使用recv函数接收服务器的响应,并使用printf函数打印响应内容。
延时和关闭连接:函数中使用vTaskDelay函数进行延时,然后使用closesocket函数关闭socket连接。
 跟进去是这样。
 
 static const uint8_t get_buf[] = "GET / HTTP/1.1 \r\nHost: www.gov.cn\r\n\r\n";
 
 
 修改
 static const uint8_t get_buf[] = "GET /cityjson HTTP/1.1 \r\nHost: ip.geo.iqiyi.com\r\n\r\n";
 
 
 
 
 结果 还是不正确 错误400请求 不知道哪来的,可能是http设置的有问题。复制代码Host:ip.geo.iqiyi.com, Server ip Address : 123.126.131.45:80␍␊
Http client connect server success!␍␊
Press CTRL-C to exit.␍␊
HTTP/1.1 200 OK␍␊
Content-Type: text/plain; charset=utf-8␍␊
Transfer-Encoding: chunked␍␊
Connection: keep-alive␍␊
Vary: Accept-Encoding␍␊
Server: APISIX/2.0␍␊
Server: QWS␍␊
Date: Fri, 22 Dec 2023 15:04:02 GMT␍␊
Access-Control-Allow-Origin: *␍␊
␍␊
d7␍␊
var returnIpCity ={"code":"A00000", "data": {"country":"", "province":"", "city":"", "country_id":0, "province_id":0, "city_id":0, "location_id":0, "isp_id":0, "isp":"", "longitude":0, "latitude":0, "ip":"0.0.0.0"}}
让ai 写个代码 3,2,1就好了
 
 复制代码#include <stdio.h>
#include <time.h>
#include <string.h>
#define MAX_HEADER_SIZE 1024
char* create_http_request_header(const char* method, const char* host, const char* path)
{
    time_t now;
    struct tm *gmt;
    char time_str[64];
    static char header[MAX_HEADER_SIZE];
    // 获取当前时间
    now = time(NULL);
    gmt = gmtime(&now);
    // 将时间格式化为HTTP头中的日期格式
    strftime(time_str, sizeof(time_str), "%a, %d %b %Y %H:%M:%S GMT", gmt);
    // 创建HTTP请求头
    snprintf(header, MAX_HEADER_SIZE, "%s %s HTTP/1.1\r\n"
                                       "Host: %s\r\n"
                                       "User-Agent: MyClient\r\n"
                                       "Accept: */*\r\n"
                                       "Date: %s\r\n"
                                       "\r\n",
             method, path, host, time_str);
    return header;
}
int main()
{
    char* request_header = create_http_request_header("GET", "www.example.com", "/");
    printf("%s", request_header);
    return 0;
}
 
 |