如何使用lwIP堆栈发送简单的HTTP请求?

问题描述 投票:7回答:2

如果问题不相关,请移动/关闭此项。

核心:Cortex-M4

微处理器:TI TM4C1294NCPDT。

IP堆栈:lwIP 1.4.1

我正在使用这个微处理器进行一些数据记录,我想通过以下形式通过HTTP请求将一些信息发送到单独的Web服务器:

http://123.456.789.012:8800/process.php?data1=foo&data2=bar&time=1234568789

我希望处理器能够看到响应头(即如果它是200 OK或出现问题) - 它不必显示/接收实际内容。

lwIP有一个用于微处理器的http服务器,但我正好相反(微处理器是客户端)。

我不确定数据包如何与请求/响应头相关联,所以我不确定我是如何实际发送/接收信息的。

http ip embedded packet lwip
2个回答
16
投票

这最终实现起来非常简单,忘了更新这个问题。

我几乎遵循了this网站上给出的指令,这是Raw / TCP'文档'。

基本上,HTTP请求是用TCP数据包编码的,所以为了将数据发送到我的PHP服务器,我使用TCP数据包发送了一个HTTP请求(lwIP完成所有工作)。

我想发送的HTTP数据包如下所示:

HEAD /process.php?data1=12&data2=5 HTTP / 1.0

主持人:mywebsite.com

要将此“翻译”为HTTP服务器可以理解的文本,您必须在代码中添加“\ r \ n”回车/换行符。所以它看起来像这样:

char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";

注意,结尾有两批“\ r \ n”

您可以使用GET或HEAD,但因为我不关心我的PHP服务器返回的HTML站点,所以我使用了HEAD(它在成功时返回200 OK,或者在失败时返回不同的代码)。

lwIP raw / tcp适用于回调。您基本上设置了所有回调函数,然后将您想要的数据推送到TCP缓冲区(在这种情况下,上面指定的TCP字符串),然后告诉lwIP发送数据包。

设置TCP连接的功能(每次我想发送TCP数据包时,我的应用程序都会直接调用此函数):

void tcp_setup(void)
{
    uint32_t data = 0xdeadbeef;

    /* create an ip */
    struct ip_addr ip;
    IP4_ADDR(&ip, 110,777,888,999);    //IP of my PHP server

    /* create the control block */
    testpcb = tcp_new();    //testpcb is a global struct tcp_pcb
                            // as defined by lwIP


    /* dummy data to pass to callbacks*/

    tcp_arg(testpcb, &data);

    /* register callbacks with the pcb */

    tcp_err(testpcb, tcpErrorHandler);
    tcp_recv(testpcb, tcpRecvCallback);
    tcp_sent(testpcb, tcpSendCallback);

    /* now connect */
    tcp_connect(testpcb, &ip, 80, connectCallback);

}

建立与PHP服务器的连接后,lwIP会调用'connectCallback'函数:

/* connection established callback, err is unused and only return 0 */
err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
    UARTprintf("Connection Established.\n");
    UARTprintf("Now sending a packet\n");
    tcp_send_packet();
    return 0;
}

此函数调用发送HTTP请求的实际函数tcp_send_packet(),如下所示:

uint32_t tcp_send_packet(void)
{
    char *string = "HEAD /process.php?data1=12&data2=5 HTTP/1.0\r\nHost: mywebsite.com\r\n\r\n ";
    uint32_t len = strlen(string);

    /* push to buffer */
        error = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);

    if (error) {
        UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", error);
        return 1;
    }

    /* now send */
    error = tcp_output(testpcb);
    if (error) {
        UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", error);
        return 1;
    }
    return 0;
}

一旦发送了TCP数据包(如果您希望“希望最好”并且不关心实际发送的数据,则需要这一切),PHP服务器返回一个TCP数据包(200 OK等等) HTML代码,如果您使用GET而不是HEAD)。可以使用以下代码读取和验证此代码:

err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
    UARTprintf("Data recieved.\n");
    if (p == NULL) {
        UARTprintf("The remote host closed the connection.\n");
        UARTprintf("Now I'm closing the connection.\n");
        tcp_close_con();
        return ERR_ABRT;
    } else {
        UARTprintf("Number of pbufs %d\n", pbuf_clen(p));
        UARTprintf("Contents of pbuf %s\n", (char *)p->payload);
    }

    return 0;
}

p-> payload包含实际的“200 OK”等信息。希望这有助于某人。

我在上面的代码中省略了一些错误检查以简化答案。


0
投票

看看维基百科中的HTTP example。客户端将发送GET和HOST线路。服务器将响应许多行以进行响应。第一行将包含响应代码。

© www.soinside.com 2019 - 2024. All rights reserved.