首先解决tcp模拟http get文件,整整浪费了一周时间:
废话不多说,http请求头长这样:
GET /ota/wifi/test/magpie.bin HTTP/1.1
Host: powerview.oss-cn-hangzhou.aliyuncs.com
Accept:*/*
Connection:Keep-Alive
特别提醒,最后1行空白必须存在,也就是一定要有一行空白
搞了个大乌龙,tcp发送get的长度没检查,原来一直是0,直到自己搭建了http本地服务器才发现这个坑
这是python写的http服务器
def httpserver(self): server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("", 8000)) server_socket.listen(128) while True: try: client_socket, client_address = server_socket.accept() print("[%s, %s]用户连接上了" % client_address) request_data = client_socket.recv(1024) print("request data:", request_data) # 构造响应数据 response_start_line = "HTTP/1.1 200 OK\r\n" response_headers = "Server: My server\r\n" response_body = "Python HTTP Test
" response = response_start_line + response_headers + "\r\n" + response_body # 向客户端返回响应数据 client_socket.send(bytes(response, "utf-8")) # 关闭客户端连接 client_socket.close() except Exception as err: print(err)
然后c语言的http get
err = TCPConnect(1, "192.168.2.142"/*server*/, 8000/*port*/);
if(err >= 0){
//fail_cnt = 0;
portal_conn.sock_fd = err;
portal_conn.status = portalsConnected;
printf("connect server ok\r\n");
memset(portal_tx_buffer, 0, 113);
memcpy(portal_tx_buffer, "GET http://www.192.168.2.142/favicon.ico HTTP/1.1\r\n",51);
memcpy(portal_tx_buffer+51, "Host:www.192.168.2.142\r\n",24);
memcpy(portal_tx_buffer+75, "Accept:*/*\r\n",12);
memcpy(portal_tx_buffer+87, "Connection:keep-alive\r\n",24);
memcpy(portal_tx_buffer+110 ,"\r\n",2);
TCPSend(portal_conn.sock_fd, portal_tx_buffer, 113);
osDelay(200);
while(1)
{
err = TCPRecv(portal_conn.sock_fd, rec, 256);
if(err<=0)
{
return err;
}
}