向WEB服务器GET,POST数据(C++代码)

致谢:感谢CSDN的zjh824。

以下代码在您的文章《  wince 中使用HttpSendRequest函数POST数据时应该注意的一点》的基础上进行了修改。

http://blog.csdn.net/zjh824/archive/2007/10/26/1845580.aspx

 http://hi.baidu.com/%B3%AC%C8%BBa/blog/item/0a2a0d1f311ad7e2e1fe0b74.html

/****************************************************************************************************************************/

BOOL SendDataToServer(CString strWeb,                                //服务端页面地址。如:www.safewe.com/job1.htm
                        CString strSendBuffer,                          //发送数据
                        LPBYTE lpRecvBuffer,                            //返回数据保存地址
                        DWORD dwRecvDataLen,                            //返回数据长度
                        CString &strRetHeadInfo,                        //返回数据头
                        CString strOpenRequestType = TEXT("GET"),       //数据请求类型:分为GET 和 POST两种
                        CString strHeaders = TEXT("Accept: */*\r\nContent-Type: application/x-www-form-urlencoded"),
                                                                        //数据头
                        DWORD dwPort = 80,                              //端口
                        DWORD dwServiceType = AFX_INET_SERVICE_HTTP     //服务类型
                        );

BOOL SendDataToServer(CString strWeb,                //服务端页面地址。如:www.safewe.com/job1.htm
                      CString strSendBuffer,         //发送数据
                      LPBYTE lpRecvBuffer,           //返回数据保存地址
                      DWORD dwRecvDataLen,           //返回数据长度
                      CString &strRetHeadInfo,       //返回数据头
                      CString strOpenRequestType,    //数据请求类型:分为GET 和 POST两种
                      CString strHeaders,            //数据头
                      DWORD dwPort,                  //端口
                      DWORD dwServiceType            //服务类型
      )
{
    CString strServerName = strWeb;
    CString strObject = TEXT("");
    HINTERNET hOpen; 
    HINTERNET hConnect;
    HINTERNET hRequest;  
    BOOL bRet = FALSE;
    DWORD dwSize;
    TCHAR* lpHeaders = NULL;
    TCHAR szHttpStatus[4] = {0};
    int dwHttpStatus = 0;
    char *pszData = NULL;
    int nErrorCode;

    int n = strWeb.Find(TEXT("/"));
    if (n != -1)
    {
        strServerName = strWeb.Left(n);
        strObject = strWeb.Mid(n+1);
    }

    hOpen = InternetOpen(TEXT("Safewe"), INTERNET_OPEN_TYPE_DIRECT, NULL, 0, 0);
    if (!hOpen)
        return FALSE;
    
    if (!(hConnect = InternetConnect(hOpen, strServerName, dwPort,  NULL, NULL, dwServiceType, 0, 0)))
    {
        InternetCloseHandle(hOpen);
        return FALSE;
    }
    
    LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};
    DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
    
    if (!(hRequest = HttpOpenRequest (hConnect, strOpenRequestType, strObject, HTTP_VERSION, NULL, 
        (LPCTSTR*)AcceptTypes, dwFlags, 0)))
    {
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hOpen);
        return FALSE;
    }
    
    DWORD dwHttpTimeout = 5000;
    if(!InternetSetOption(hRequest,INTERNET_OPTION_CONNECT_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
        goto END;
    if(!InternetSetOption(hRequest,INTERNET_OPTION_SEND_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
        goto END;
    if(!InternetSetOption(hRequest,INTERNET_OPTION_RECEIVE_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
        goto END;
    
    //重点要注意的地方, HttpSendRequest 的第三个参数必须为多字节编码格式,否则服务器端会出错
    pszData = new char[strSendBuffer.GetLength()*2+2];
    memset(pszData,0,strSendBuffer.GetLength()*2+2);
    WideCharToMultiByte( CP_ACP, 0, strSendBuffer.GetBuffer(0),
        -1, pszData, strSendBuffer.GetLength()*2, NULL, NULL);
    
//     HttpAddRequestHeaders(hRequest, szHeaders, szHeaders.GetLength(), HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA );//数据报头这样添加也可以
    if(HttpSendRequest(hRequest, strHeaders, strHeaders.GetLength(), pszData, strlen(pszData)) == FALSE)
    {
        nErrorCode = GetLastError();
        goto END;
    }//如果数据报头 strHeaders 为空,会产生 ERROR_HTTP_HEADER_NOT_FOUND 错误
    
    HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);    
    lpHeaders = new TCHAR [dwSize+1];
    ZeroMemory(lpHeaders, sizeof(lpHeaders)/sizeof(TCHAR));
    if (!HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpHeaders, &dwSize, NULL))
        goto END2;
    
    strRetHeadInfo = lpHeaders;
    
    memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
    dwHttpStatus =_wtoi(szHttpStatus);
    if ((dwHttpStatus<200) || (dwHttpStatus>300))
        goto END2;
    
    if(!InternetReadFile (hRequest, (LPVOID)(lpRecvBuffer), dwRecvDataLen, &dwSize))//这里要接收所有数据,可改为循环接收
        goto END2;
    if(dwSize == 0)
        goto END2;
    
    bRet = TRUE;
END2:
    if (lpHeaders)
    {
        delete lpHeaders;
        lpHeaders= NULL;
    }
END:
    if (pszData)
    {
        delete pszData;
        pszData= NULL;
    }
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hOpen);
    return bRet;
}

/****************************************************************************************************************************/

//函数使用示例:

    BYTE byRecvBuff[1024] = {0};
    CString strRetHead;
    SendDataToServer(TEXT("update.safewe.com/report.php"), 
                     TEXT("string=0123456789012345test&"), byRecvBuff, 1024, strRetHead, TEXT("POST"));

/****************************************************************************************************************************/

//对应的PHP页面代码:

<html>
<body>

Thanks for your report!<br />

<?php
$md5 = $_POST["string"];
$file=fopen("data.txt", "a+") or $file=fopen("data.txt", "w");
fwrite($file, $string, strlen($string));
fclose($file);
?>

</body>
</html>

/****************************************************************************************************************************/


 

参考文章:

《  wince 中使用HttpSendRequest函数POST数据时应该注意的一点》

http://blog.csdn.net/zjh824/archive/2007/10/26/1845580.aspx

你可能感兴趣的:(C++,String,null,web服务,internet,WinCE)