假设页面为:login.asp
 <%
            User= Trim(Request.Form("User"))
           Password = Trim(Request.Form("Password"))
           'User = Trim(Request.QueryString("User"))
           'Password = Trim(Request.QueryString("Password"))
           Response.Write "User = "& User 
           Response.Write "Password = "& Password
%> 
     
请问怎样向HTTP服务器POST数据,并得到服务器响应结果?
用WinHTTP实现

解决方案 »

  1.   

    用下面的mfc类函数可以实现
    CInternetSession::GetHttpConnection
    CHttpConnection::OpenRequest
    CHttpFile::SendRequest
    CHttpFile::Read
      

  2.   

    谢谢!我现在用的是WinHTTP,这怎么解决呢?
    // Use WinHttpOpen to obtain a session handle.
        hSession = WinHttpOpen(L" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)", 
    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
    WINHTTP_NO_PROXY_NAME, 
    WINHTTP_NO_PROXY_BYPASS, 0);

        // Specify an HTTP server.
        if (hSession)
            hConnect = WinHttpConnect(hSession, L"robert.game.com",
    INTERNET_DEFAULT_HTTP_PORT, 0);

        // Create an HTTP Request handle.
        if (hConnect)
            hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/post.asp", 
    NULL, L"Content-Type: application/x-www-form-urlencoded", 
    WINHTTP_DEFAULT_ACCEPT_TYPES,
    0);    // Send a Request.
        if (hRequest) 
            bResults = WinHttpSendRequest(hRequest, 
    WINHTTP_NO_ADDITIONAL_HEADERS, 0,
    pszData, strlen(pszData), 
    strlen(pszData), 0);   // End the request.
        if (bResults)
            bResults = WinHttpReceiveResponse(hRequest, NULL);  if(bResults)
    {
    do 
    {
    // Check for available data.
    dwSize = 0;
    if(!WinHttpQueryDataAvailable(hRequest, &dwSize))
    AfxMessageBox("Error %u in WinHttpQueryDataAvailable.\n",
                    GetLastError());

    // Allocate space for the buffer.
    pszOutBuffer = new char[dwSize+1];
    if(!pszOutBuffer)
    {
    AfxMessageBox("Out of memory\n");
    dwSize=0;
    }
    else
    {
    // Read the data.
    ZeroMemory(pszOutBuffer, dwSize+1);

    if( !WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, 
    dwSize, &dwRead ))
    AfxMessageBox("Error %u in WinHttpReadData.\n", GetLastError( ));
    else
    AfxMessageBox(pszOutBuffer);

    // Free the memory allocated to the buffer.
    delete[] pszOutBuffer;
    }
    } while( dwSize > 0 );
    }
        // Report any errors.
        if (!bResults)
    {
    char strbuf[125];
    sprintf(strbuf, "Error %d has occurred.\n",GetLastError());
            AfxMessageBox(strbuf);
    }

        // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
      

  3.   

    可以看看HTTP协议,HTTP协议有几种方式 Get  post head 几种,,如果想Post数据就要用Post的方式向指定的页面就好了
      

  4.   

    谢谢大家!我的问题解决了。
    如果要向页面提交表单,要加Http头"Content-Type:application/x-www-form-urlencoded"
    如果要向页面提交表单和上传文件,要加Http头“Content-Type: application/x-www-form-urlencoded”
      

  5.   

    用下面的mfc类函数可以实现 
    CInternetSession::GetHttpConnection 
    CHttpConnection::OpenRequest 
    CHttpFile::SendRequest 
    CHttpFile::Read  具体怎么实现呢 请给我详细代码好不 谢谢了 很急