简体   繁体   English

发送带有XML内容的POST http请求

[英]Send a POST http request with XML content

I like to send a http POST request and a XML content to my sensor : 我喜欢将http POST请求和XML内容发送到我的传感器:

For the counting data: 对于计数数据:

http://{SensorIP}:5211/1.0-PAI/PassengerCountingService/SubscribeAllData

The XML data: XML数据:

<SubscribeRequest>
    <Client-IP-Address>
       <Value>{HostIP}</Value>
    </Client-IP-Address>
    <ReplyPort>
       <Value>{YourReplyPort}</Value>
     </ReplyPort>
     <ReplyPath>
          <Value>{YourReplyPath}</Value>
     </ReplyPath>
</SubscribeRequest>

I have a socket TCP for connecting to the sensor but i Don't khnow how sending this XML format with POST operation. 我有一个用于连接传感器的套接字TCP,但我不知道如何通过POST操作发送这种XML格式。

Have-you an idea ? 你有个主意吗? Edited: 编辑:

 char buffer[1024] = "";
    int nOctetsRecus = 0;
    int nOctetsEnvoyes = 0;
    int tailleBuffer = 0;
    int boucle = 1;
    char * URL;
    char * xml_data;
    nOctetsRecus = 0;
    nOctetsEnvoyes = 0;
    tailleBuffer = 0;
    boucle = 1;

    //STEP1: Initialisation Winsock
    iwsaStartup = WSAStartup(MAKEWORD(2,2), &winSockData);
    if (iwsaStartup != 0)
    {
        printf("APICP: open_socket(): WSAStratup Failed\r\n");
    }
    //LOG_INFO("APICP: opensocket(): WSAStarup Success\r\n");

    //STEP3: Socket UDPSocketClient Creation
    UDPSocketClient = socket(AF_INET, SOCK_DGRAM, 0);
    if (UDPSocketClient == INVALID_SOCKET)
    {
        printf("Socket creation failed with error: %d\r\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    printf("La socket %d est ouverte en TCP/IP.\n", UDPSocketClient);
    // on configure l'interface de connexion
    IRMAServer.sin_addr.s_addr = inet_addr(Addr_TCP_IRMA); // adresse IP du serveur IRMA
    IRMAServer.sin_family = AF_INET;
    IRMAServer.sin_port = htons(Port_TCP_IRMA); // sin_port et sin_addr doivent être en Network Byte Order // htons() convertit
                                            //un entier court depuis l'ordre des octets de l'hôte vers celuidu réseau (host to network short)
    //printf("Connexion a %s sur le port %d\n", inet_ntoa(IRMAServer.sin_addr), htons(IRMAServer.sin_port));
    if(connect(UDPSocketClient, (SOCKADDR*)&IRMAServer, iIRMAServerLen) != SOCKET_ERROR) // si le client réussit à se connecter au serveur
        printf("Connexion a %s sur le port %d\n", inet_ntoa(IRMAServer.sin_addr), htons(IRMAServer.sin_port));
    else
        printf("Erreur de connexion.");

    // on reçoit les données envoyées par le serveur
    /*nOctetsRecus = recv(UDPSocketClient, buffer, 1024, 0);
    if(nOctetsRecus != SOCKET_ERROR)
    {
        printf("%d octets ont ete recus\n", nOctetsRecus);
        printf("texte recu : %s", buffer);
    }
    else
        printf("Les donnees n'ont pas ete recues");*/
    // le client envoie des données au serveur
    URL="/1.0-PAI/PassengerCountingService/SubscribeAllData";
    xml_data="<SubscribeRequest> <Client-IP-Address> <Value>192.168.X.X</Value> </Client-IP-Address> <ReplyPort></Value> </ReplyPort> <ReplyPath> <Value>C:\</Value> </ReplyPath> </SubscribeRequest>";
    tailleBuffer = sprintf(buffer, " POST %s HTTP/1.1\r\nHost:192.168.0.168:5211\r\nAccept:text/xml\r\nContent-Type:text-xml\r\nContent-Length:1024\r\n\r\n%s"
                          ,URL, xml_data);
    nOctetsEnvoyes = send(UDPSocketClient, buffer, tailleBuffer, 0);
    if(nOctetsEnvoyes != SOCKET_ERROR)
    {
        printf("%d octets envoyes au serveur\n", nOctetsEnvoyes);
        printf("texte envoye : %s", buffer);
    }
    else
        printf("Erreur : donnees non envoyees au serveur");

            // on reçoit les données envoyées par le serveur
    nOctetsRecus = recv(UDPSocketClient, buffer, 1024, 0);
    if(nOctetsRecus != SOCKET_ERROR)
    {
        printf("%d octets ont ete recus\n", nOctetsRecus);
        printf("texte recu : %s", buffer);
    }
    else
        printf("Les donnees n'ont pas ete recues");


    // on ferme la connexion
    closesocket(UDPSocketClient);
    WSACleanup(); // libère les ressources allouées par la fonction WSAStartup()

getchar();

return 0;

enter image description here 在此输入图像描述

Have-you an idea why i have this ICMP message? 你知道为什么我有这个ICMP消息吗?

Just send this string by tcp socket by port 5211: 只需通过端口5211通过tcp socket发送此字符串:

POST /1.0-PAI/PassengerCountingService/SubscribeAllData HTTP/1.1
Host: SensorIP
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: */*
Content-Type: text-xml
Content-Length: {strlen(THIS)}

<SubscribeRequest>
    <Client-IP-Address>
       <Value>{HostIP}</Value>
    </Client-IP-Address>
    <ReplyPort>
       <Value>{YourReplyPort}</Value>
     </ReplyPort>
     <ReplyPath>
          <Value>{YourReplyPath}</Value>
     </ReplyPath>
</SubscribeRequest>

Between headers and content are one free line so: \\r\\n\\r\\n . 标题和内容之间是一个空行,所以: \\r\\n\\r\\n Remember about Content-Length . 记住Content-Length Basic info about HTTP: https://www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html 有关HTTP的基本信息: https//www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM