bool CAmHttpSocket::PostUrl(const TCHAR *url, const char *PostData, int PostDataLength)
{
//check length of postdata
if (PostDataLength == -1)
PostDataLength = strlen(PostData);
//some variable that we need...
URL_COMPONENTS uc;
//let's split the url...
uc.dwStructSize = sizeof(uc);
uc.lpszScheme = NULL;
uc.dwSchemeLength = 0;
uc.lpszHostName = NULL;
uc.dwHostNameLength = 1;
uc.nPort = 0;
uc.lpszUserName = NULL;
uc.dwUserNameLength = 0;
uc.lpszPassword = NULL;
uc.dwPasswordLength = 0;
uc.lpszUrlPath = NULL;
uc.dwUrlPathLength = 1;
uc.lpszExtraInfo = NULL;
uc.dwExtraInfoLength = 0;
InternetCrackUrl(url, _tcslen(url), 0, &uc);
//post the data...
if (hCO != NULL) InternetCloseHandle(hCO);
TCHAR *HostName = _tcsdup(uc.lpszHostName);
HostName[uc.dwHostNameLength] = '\0';
TCHAR *FileName = _tcsdup(uc.lpszUrlPath);
FileName[uc.dwUrlPathLength] = '\0';
if (hIS != NULL) InternetCloseHandle(hIS); //if open, close the handle to the connection
DWORD flags;
if (uc.nPort == 80)
{
//we are talking plain http
flags = INTERNET_FLAG_NO_CACHE_WRITE;
}
else
{
//we are talking secure https
flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
}
TCHAR headers[] = _T("Content-Type: application/x-www-form-urlencoded"); //content type for post...
TCHAR szAccept[] = _T("*/*"); //we accept everything...
LPTSTR AcceptTypes[2]={0};
AcceptTypes[0]=szAccept;
hCO = InternetConnect(hIO, HostName, uc.nPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
hIS = HttpOpenRequest(hCO, _T("POST"), FileName, NULL, NULL, (LPCTSTR*)AcceptTypes, flags, 0);
if (!HttpSendRequest(hIS, headers, _tcslen(headers), (TCHAR*)PostData, PostDataLength))
{
LastError = GetLastError();
free(HostName);
free(FileName);
return false;
}
free(HostName);
free(FileName);
return true;
}
see this url
http://www.codeproject.com/useritems/amhttputils.asp

解决方案 »

  1.   

    bool CAmHttpSocket::PostUrl(const TCHAR *url, const char *PostData, int PostDataLength)
    {
    //check length of postdata
    if (PostDataLength == -1)
    PostDataLength = strlen(PostData);
    //some variable that we need...
    URL_COMPONENTS uc;
    //let's split the url...
    uc.dwStructSize = sizeof(uc);
    uc.lpszScheme = NULL;
    uc.dwSchemeLength = 0;
    uc.lpszHostName = NULL;
    uc.dwHostNameLength = 1;
    uc.nPort = 0;
    uc.lpszUserName = NULL;
    uc.dwUserNameLength = 0;
    uc.lpszPassword = NULL;
    uc.dwPasswordLength = 0;
    uc.lpszUrlPath = NULL;
    uc.dwUrlPathLength = 1;
    uc.lpszExtraInfo = NULL;
    uc.dwExtraInfoLength = 0;
    InternetCrackUrl(url, _tcslen(url), 0, &uc);
    //post the data...
    if (hCO != NULL) InternetCloseHandle(hCO);
    TCHAR *HostName = _tcsdup(uc.lpszHostName);
    HostName[uc.dwHostNameLength] = '\0';
    TCHAR *FileName = _tcsdup(uc.lpszUrlPath);
    FileName[uc.dwUrlPathLength] = '\0';
    if (hIS != NULL) InternetCloseHandle(hIS); //if open, close the handle to the connection
    DWORD flags;
    if (uc.nPort == 80)
    {
    //we are talking plain http
    flags = INTERNET_FLAG_NO_CACHE_WRITE;
    }
    else
    {
    //we are talking secure https
    flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_SECURE |
    INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
    }
    TCHAR headers[] = _T("Content-Type: application/x-www-form-urlencoded"); //content type for post...
    TCHAR szAccept[] = _T("*/*"); //we accept everything...
    LPTSTR AcceptTypes[2]={0};
    AcceptTypes[0]=szAccept;
    hCO = InternetConnect(hIO, HostName, uc.nPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
    hIS = HttpOpenRequest(hCO, _T("POST"), FileName, NULL, NULL, (LPCTSTR*)AcceptTypes, flags, 0);
    if (!HttpSendRequest(hIS, headers, _tcslen(headers), (TCHAR*)PostData, PostDataLength))
    {
    LastError = GetLastError();
    free(HostName);
    free(FileName);
    return false;
    }
    free(HostName);
    free(FileName);
    return true;
    }
    see this
    http://www.codeproject.com/useritems/amhttputils.asp