jolee 发表于 2013-7-25 11:19

HTTP-POST方法内容长度过大,多次发送的问题

没找到合适分类发帖区,暂借宝地,谢谢!
------------------------
请教HTTP-POST方法使用时,当要上传内容长度Content-Length超过IP包长度限制如10000,一般是如何处理的?
{
"POST /ems/php/emud.php HTTP/1.0\r\n"
"Host: 192.168.52.69\r\n"
"Connection: close\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 10000\r\n"
"\r\n"
"information=xxxxxxxxxxxxxxxxx"
};

可以把要发送的内容拆分成:头字符串/数据内容/数据内容/数据内容... 再多次调用send()发送出去,发送完所有数据再close(),这样可行吗?发送头字符串时,Connection: 是否要定义为Keep-Alive?

感谢!

maztower 发表于 2013-8-5 10:39

调用send后,实际发送的数据量不一定等于设定的数据长度,原因是网络拥塞,缓冲区满或其他异常
实现一个safesend函数,如下
int SafeSend(SOCKET sock, Uint8* pBuf, int nLen)
{
        int nRet = 0;
        int nSended = 0;
        int ntry = 0;
        while (nSended < nLen) {
                nRet = send(sock, pBuf + nSended, nLen - nSended, 0);
                if (nRet <= 0) {
                        if (ntry < 5) {
                                TSK_sleep(1);
                                ntry++;
                                continue;
                        }else {
                                break;
                        }
                }
                ntry = 0;
                nSended += nRet;
        }
       
        if (nSended < nLen) {
                return -1;
        }
        return nSended;
}

页: [1]
查看完整版本: HTTP-POST方法内容长度过大,多次发送的问题