void CUpLoadDlg::FUNC()
{   
    CInternetSession     m_InetSession("session");
    CHttpConnection*     pServer = NULL;
    CHttpFile*     pFile = NULL;
    CString  m_strHtml;
    CString  m_strRequest = "NAME=file1&ENCTYPE=multipart/form-data&wd=+&action=upload&data=c:\\11.jpg&fn=1.jpg";
    CString  m_strServerName = "121.34.44.252";
    CString  m_strObjectName = "/111/system/123.asp";
    char szHeaders[100] = "Content-Type: application/x-www-form-urlencoded";
    try
    {
        INTERNET_PORT  nPort;
        nPort = 80;
        pServer =  m_InetSession.GetHttpConnection(m_strServerName, nPort);
        pFile   =  pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, m_strObjectName);
        
        //image/pjpeg
        //strcpy(szHeaders,"Accept: text*/*\r\nContent-Type: application/x-www-form-urlencoded");
        //strcpy(szHeaders,"Content-Type: application/x-www-form-urlencoded");
        pFile->AddRequestHeaders(szHeaders);
        
        pFile->SendRequestEx(m_strRequest.GetLength());
        pFile->WriteString(m_strRequest);  //重要-->m_Request 中有"name=aaa&name2=BBB&..."
        pFile->EndRequest();
        DWORD  dwRet;
        pFile->QueryInfoStatusCode(dwRet);
        CString str;
        
        //m_Mutex.Lock();
        m_strHtml = "";
        char szBuff[1024];
        if (dwRet == HTTP_STATUS_OK)
        {
            UINT nRead;
            while ((nRead = pFile->Read(szBuff, 1023)) > 0)
            {   
                m_strHtml += CString(szBuff, nRead);
            }
            SetDlgItemText(IDC_EDIT1, m_strHtml);
        }
        //m_Mutex.Unlock();
        if (pFile)
        {
            pFile->Close();
            delete  pFile;
        }
        
        if (pServer)
        {
            pServer->Close();
            delete pServer;
        }
    }
    catch (CInternetException* e)
    {   
        CString     s;
        s.Format("Internet Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext);
        AfxMessageBox(s);
    }
}void FUNC3()
{
    static TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
    static TCHAR frmdata[] = _T("name=John+Doe&userid=hithere&other=P%26Q");
    static LPSTR accept[2]= {"*/*", NULL};
    
    // for clarity, error-checking has been removed
    HINTERNET hSession = InternetOpen("MyAgent",
        INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
        INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", _T("FormActionHere"), NULL, NULL, (const char **)accept, 0, 1);
    HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    // close any valid internet-handles
}
void   CUpLoadDlg::FUNC2()
{   
    CString   strServerName = _T("121.34.44.252");
    CString   strFormAction = _T("/111/system/123.asp");//Accept: */*\r\n
    CString   strHeaders    = _T("Content-Type: application/x-www-form-urlencoded");
    CString   strFormData   = _T("NAME=file1&ENCTYPE=multipart/form-data&wd=+&action=upload&data=c:\\11.jpg&fn=1.jpg");
    
    CInternetSession   session;
    CHttpConnection*   pConnection = session.GetHttpConnection(strServerName);
    CHttpFile*   pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strFormAction);
    BOOL   result = 0;
    
    CString   smsg;
    CStdioFile   f;
    result = pFile->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
    //result = pFile->SendRequest(NULL, NULL, 0);
    DWORD  dwRet;
    pFile->QueryInfoStatusCode(dwRet);
    f.Open("returnRequest.html", CFile::modeCreate | CFile::modeWrite | CFile::typeText);
    
    while (pFile->ReadString(smsg))           //我把返回结果写到文件:returnRequest.html   
    {   
        f.WriteString(smsg);
        f.WriteString("\n");
    }
    
    f.Close();   
    if (pFile)
    {
        pFile->Close();
        delete pFile;
    }
    
    if (pConnection)
    {
        pConnection->Close();
        delete pConnection;   
    }
    session.Close();   
}
void CUpLoadDlg::OnButtonUpload4() 
{
FUNC();
}void CUpLoadDlg::OnButtonUpload5() 
{
FUNC2();
}这是在网上抄的一段代码,编译执行上传后,没任何反映,我感觉是
CString   strFormData   = _T("NAME=file1&ENCTYPE=multipart/form-data&wd=+&action=upload&data=c:\\11.jpg&fn=1.jpg");
没写对,请大家帮忙看看,谢谢
后面是 网站的程序upload.asp文件
<%
Function GetFileName(ByVal strFile)
  If strFile <> "" Then
   GetFileName = mid(strFile,InStrRev(strFile, "\")+1)
  Else
   GetFileName = ""
  End If
End  functionstrFileName = Request.Form("file1")
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 ' adTypeBinary
objStream.Open
objStream.LoadFromFile strFileName
objStream.SaveToFile Server.MapPath(GetFileName(strFileName)),2
objStream.Close
%>123.ASP文件
<form name="FORM" action="upload.asp" method="post">
  <input type="submit" name="submit" value="OK">
     <input type="file" name="file1" style="width:400"  value="">
</form>

解决方案 »

  1.   

    Try to use a sniffer to find out what is wrong in your HTTP protocol
      

  2.   

    不明白是用VC开发WEB吗?是怎么联系在一块的?
      

  3.   

    _T("NAME=file1&ENCTYPE=multipart/form-data&wd=+&action=upload&data=c:\\11.jpg&fn=1.jpg"); 
    建议你 先用抓包工具(winsock),通过asp 直接上传,看看截获到的post到web service 上的数据是什么格式的。然后再用vc post 上去,同样也用winsock 截获,看看数据是不是一样的。
    这样可以帮你分析··
      

  4.   

    我这个就是用http post上传文件的
    http://yanghao.org/su/su.rar
      

  5.   

    这么老的帖子怎么又被翻出来了~~这没必要使用 Form的方式提交, 直接在 Http Body里面Post二进制是的。
    void CHttpClient::OpenConnection( LPCTSTR lpszServer, UINT nPort)
    {
    CloseConnection();
    m_hSession = ::InternetOpen(  USER_AGENT
    , INTERNET_OPEN_TYPE_PRECONFIG
    , NULL
    , NULL
    , 0
    );
    if( !m_hSession )
    throw CCustomException(_T("Error: Failed to connect to the server, InternetOpen failed."));
    m_hConnect = ::InternetConnect( m_hSession
    , lpszServer
    , nPort
    , NULL
    , NULL
    , INTERNET_SERVICE_HTTP
    , NULL
    , NULL
    );
    if( !m_hConnect )
    throw CCustomException(_T("Error: Failed to connect to the server, InternetConnect failed."));
    }
    void CHttpClient::CloseConnection(void)
    {
    if( m_hSession )
    {
    ::InternetCloseHandle(m_hSession);
    m_hSession = NULL;
    }
    if( m_hConnect )
    {
    ::InternetCloseHandle(m_hConnect);
    m_hConnect = NULL;
    }
    }
    void CHttpClient::PostBuffer( LPCTSTR lpszPath, LPBYTE lpBuffer, DWORD dwSize)
    {
    ASSERT( m_hSession && m_hConnect );
    HINTERNET hRequest = ::HttpOpenRequest( m_hConnect
    , _T("POST")
    , lpszPath
    , NULL
    , NULL
    , NULL
    , INTERNET_FLAG_NO_CACHE_WRITE
    , 0
    );
    if(!hRequest)
    throw CCustomException(_T("Error: Failed to POST data to server, HttpOpenRequest failed."));
    INTERNET_BUFFERS stBuffers = {0};
    stBuffers.dwStructSize = sizeof(stBuffers); 
    stBuffers.Next = NULL; 
    stBuffers.lpcszHeader = NULL;
    stBuffers.dwHeadersLength = 0;
    stBuffers.dwHeadersTotal = 0;
    stBuffers.lpvBuffer = NULL;                
    stBuffers.dwBufferLength = 0;
    stBuffers.dwBufferTotal = dwSize;
    stBuffers.dwOffsetLow = 0;
    stBuffers.dwOffsetHigh = 0;
    BOOL bRet = ::HttpSendRequestEx( hRequest, &stBuffers, NULL, 0, 0);
    if(!bRet)
    {
    ::InternetCloseHandle(hRequest);
    throw CCustomException(_T("Error: Failed to POST data to server, HttpSendRequestEx failed."));
    }
    DWORD dwSent = 0;
    DWORD dwBytesWritten = 0;
    while(dwSent < dwSize)
    {
    bRet = ::InternetWriteFile( hRequest
    , (LPBYTE)(lpBuffer + dwSent)
    , dwSize - dwSent
    , &dwBytesWritten
    );
    if( bRet )
    dwSent += dwBytesWritten;
    }
    bRet = ::HttpEndRequest(hRequest, NULL, 0, 0);
    ::InternetCloseHandle(hRequest);
    if( !bRet )
    throw CCustomException(_T("Error: Failed to POST data to server, HttpEndRequest failed."));
    }
      

  6.   

    服务端接收方式:ASP.Net:Stream stream = Page.Request.InputStream;  
    byte[] buffer = new byte[stream.Length];  
    stream.Read(buffer, 0, buffer.Length);  PHP:$post = file_get_contents("php://input"); 
    if( strlen($post) == 0 )
    {
        die("Error: missing POST body.");
    }
    file_put_contents( $file, $post);