在访问网络的时候,有时候需要认证,sdk我看不太明白。在internetsession类中有一个SETOPTION,可使用不好……CInternetSession sess;
char * pwd = "dfgasdfsdf";
char * login_name = "dsfgdsgds";
if(AuthenticationCode ==401) //server authentication
{
int nn =sess.SetOption(INTERNET_OPTION_PROXY_PASSWORD,pwd,sizeof(pwd));
if(!nn) nn = GetLastError();
int mm =sess.SetOption(INTERNET_OPTION_USERNAME,login_name,strlen(login_name)+1,0);
if(!mm) mm = GetLastError();}
返回的值12018错误:
ERROR_WINHTTP_INCORRECT_HANDLE_TYPE (12018)
The type of handle supplied is incorrect for this operation.

解决方案 »

  1.   

    这是全部的文件。#include <atlbase.h>
    #include "stdafx.h"
    #include<stdio.h>
    #include<stdlib.h>
    #include<windows.h>
    #include "FoxUpdate.h"
    #include "FoxUpdateDlg.h"
    #include "PasswordDlg.h"
    #include "AfxInet.h"
    char *pwd ="dfgsdgdfsg";
    char *login_name ="wetgwetewrt1123423";
    char  *s_svr_URl = " ";
    BOOL b_cancel = FALSE;
    int n_downloaded_file_len = 0;
    CString s_file_len = " ";
    int n_bios_type = 4 ;
    time_t nowtime = 0 , l_begin = 0 ;
    int AuthenticationCode = 401;
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    /////////////////////////////////////////////////////////////////////////////
    // CPasswordDlg dialog
    CPasswordDlg::CPasswordDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CPasswordDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CPasswordDlg)
    m_loginname = _T("");
    m_password = _T("");
    //}}AFX_DATA_INIT
    }
    void CPasswordDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CPasswordDlg)
    DDX_Control(pDX, IDOK, m_pwd_ok);
    DDX_Control(pDX, IDCANCEL, m_pwd_cancel);
    DDX_Text(pDX, IDC_EDIT1, m_loginname);
    DDX_Text(pDX, IDC_EDIT2, m_password);
    //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CPasswordDlg, CDialog)
    //{{AFX_MSG_MAP(CPasswordDlg)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CPasswordDlg message handlersDWORD WINAPI TryDldURl(LPVOID n)
    {




    CWnd* pWnd = (CWnd*)n;
    CInternetSession sess;//Create session
        
    CHttpFile* fileGet;
    time(&l_begin); try
        {
            if(!b_cancel)
    {
    fileGet=(CHttpFile*)sess.OpenURL(s_svr_URl);//Open file
         }
    }
        catch (CInternetException* pEx)
        {
              //if anything went wrong, just set file to NULL
            int nn = pWnd->PostMessage(WM_Download_Status,0,2);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet = NULL;
            pEx->Delete();
    sess.Close();
    return FALSE;
        }
      
    if(AuthenticationCode ==401) //server authentication
    {
    int nn =sess.SetOption(INTERNET_OPTION_PROXY_PASSWORD,pwd,sizeof(pwd));
    if(!nn) nn = GetLastError();
    int mm =sess.SetOption(INTERNET_OPTION_USERNAME,login_name,strlen(login_name)+1,0);
    if(!mm) mm = GetLastError();
    mm ++;
    mm++; }
    if(AuthenticationCode==407) //proxy authentication
    {
    sess.SetOption(INTERNET_OPTION_PROXY_PASSWORD,pwd,strlen(pwd)+1,0);
    sess.SetOption(INTERNET_OPTION_PROXY_USERNAME,login_name,strlen(login_name)+1,0);
    }
    try
    {
    if(fileGet)
        {
            char buffer[1024];
    DWORD dwStatus;
            DWORD dwBuffLen = sizeof(dwStatus);
            BOOL bSuccess; if(!b_cancel)
    {
    bSuccess = fileGet->QueryInfo(
    HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
    &dwStatus, &dwBuffLen);
    }
    if(bSuccess && ((dwStatus==401) || (dwStatus==407) ) )
    {
    AuthenticationCode = dwStatus;
    int nn = pWnd->PostMessage(WM_Download_Status,0,3);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    sess.Close();
    return FALSE;
    }
    if( bSuccess && (dwStatus>=  200&& dwStatus<300)  && (!b_cancel)) 
            { 
    BOOL b_header = fileGet->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,s_file_len,NULL);
    if(!b_header)
    {
    int nn = pWnd->PostMessage(WM_Download_Status,0,2);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    sess.Close();
    return FALSE;
    }
    else
    {
    int n_bios_len = atoi(s_file_len);
    //Check BIOS bin file size
    if( n_bios_len != (131072*n_bios_type) )
    {
    BOOL b_send_msg_ok = pWnd->PostMessage(WM_Download_Status,0, 8);
    if(!b_send_msg_ok)pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    sess.Close();
    return FALSE;
    }


    BOOL b_send_msg_ok = pWnd->PostMessage(WM_Download_Status,1, 1);
    if(!b_send_msg_ok)
    {
    pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    sess.Close();
    return FALSE;
    }
    }

    CStdioFile     fileWrite; 
    if(fileWrite.Open("temp.bin", CFile::modeWrite|CFile::modeCreate
    |CFile::typeBinary))//Warning:can not write file property just because of open Mode!!
                {
                    int i = 0;
    n_downloaded_file_len = 0;
    if(!b_cancel)
    {
    i = fileGet->Read(buffer,1024);
    n_downloaded_file_len = n_downloaded_file_len + i;
    BOOL b_send_msg_ok = pWnd->PostMessage(WM_Download_Status,1, 3);
    if(!b_send_msg_ok)
    {
    pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    fileWrite.Close(); 
    sess.Close();
    return FALSE;
    }
    while(i == 1024)
    {
    if(b_cancel)break;
    fileWrite.Write(buffer,i); i = fileGet->Read(buffer,1024);
    n_downloaded_file_len = n_downloaded_file_len + i;
    BOOL b_send_msg_ok = pWnd->PostMessage(WM_Download_Status,1, 2);
    if(!b_send_msg_ok)
    {
    pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    fileWrite.Close(); 
    sess.Close();
    return FALSE;
    }
    }
    if((i!=1024) && (i>0) && (!b_cancel))
    {
    fileWrite.Write(buffer,i);
    BOOL b_send_msg_ok = pWnd->PostMessage(WM_Download_Status,1, 5);
    if(!b_send_msg_ok)
    {
    pWnd->PostMessage(WM_Download_Status,0,7);
    fileGet->Close();
    delete fileGet;
    fileWrite.Close(); 
    sess.Close();
    return FALSE;
    }
    }

    }
    fileWrite.Close();
                }
    else
    {
    int nn = pWnd->PostMessage(WM_Download_Status,0,6);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    sess.Close();
    fileGet->Close();
    delete fileGet;
    return FALSE;
    }
    fileGet->Close();
    delete fileGet;
    }
    else
    {
    int nn = pWnd->PostMessage(WM_Download_Status,0,1);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    sess.Close();
    fileGet->Close();
    delete fileGet;
    return FALSE;
    }
    }
    else
    {
    int nn = pWnd->PostMessage(WM_Download_Status,0,5);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    sess.Close();
    return FALSE;
    }
    }
    catch(CInternetException* pEx)
    {
    //Hardware fails!
    int nn = pWnd->PostMessage(WM_Download_Status,0,4);
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    pEx->Delete();
    if(fileGet)
    {
    fileGet->Close();
    delete fileGet;
    }
    sess.Close();
    return FALSE;
    }
    sess.Close();
    int nn;
    if(b_cancel)
    {
    nn = pWnd->PostMessage(WM_Download_Status,0,0);
    }
    else
    {
    nn = pWnd->PostMessage(WM_Download_Status,1,4);
    }
    if (!nn)pWnd->PostMessage(WM_Download_Status,0,7);
    return TRUE;
    }
      

  2.   

    Part 2/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CFoxUpdateDlg dialogCFoxUpdateDlg::CFoxUpdateDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CFoxUpdateDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CFoxUpdateDlg)
    m_connect_status = _T("Not connected!");
    m_Speed = _T("0.00");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CFoxUpdateDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CFoxUpdateDlg)
    DDX_Control(pDX, IDCANCEL, m_button_cancel);
    DDX_Control(pDX, IDC_download, m_button_download);
    DDX_Text(pDX, IDC_status, m_connect_status);
    DDX_Text(pDX, IDC_speed, m_Speed);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CFoxUpdateDlg, CDialog)
    //{{AFX_MSG_MAP(CFoxUpdateDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_link, Onlink)
    ON_BN_CLICKED(IDC_download, Ondownload)
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_Download_Status, Do_Status_Change)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CFoxUpdateDlg message handlersBOOL CFoxUpdateDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CFoxUpdateDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CFoxUpdateDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CFoxUpdateDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
      

  3.   

    part3
    void CFoxUpdateDlg::Ondownload() 
    {
    // TODO: Add your control notification handler code here
      m_button_download.ModifyStyle(WS_VISIBLE,WS_DISABLED,0); 
    m_button_cancel.ModifyStyle(WS_DISABLED,WS_VISIBLE,0);
    CFoxUpdateDlg::RedrawWindow();  //s_svr_URl = "http://download.microsoft.com/download/win2000platform/SP/SP3/NT5/CN/W2Ksp3.exe";
    s_svr_URl = "http://www.qdigrp.com/qdisite/driver/W1E_v21slc.ZIP";
    //s_svr_URl = "http://10.161.21.64/dccweb/uploadfiles/191/6A79AFKA.BIN";

    HANDLE hThrd;
    DWORD threadId;
    b_cancel = FALSE;
    hThrd = CreateThread(NULL,0,TryDldURl,(LPVOID)this,0,&threadId);
    if(!hThrd)
    {
    m_connect_status = "can not create a worker for download...";
    UpdateData(false);
    } m_connect_status = "downloading ...";
    UpdateData(false);}void CFoxUpdateDlg::Onlink() 
    {
    // TODO: Add your control notification handler code here
    MessageBox("Call ok!");
    }void CFoxUpdateDlg::Do_Status_Change(UINT nID, LPARAM lParam) 
    {
    // TODO: Add your control notification handler code here
    if(nID)
    {
    time(&nowtime);

    char s_downloaded_file_len[12];
    _itoa(n_downloaded_file_len, s_downloaded_file_len, 10 );
    CString s_status_out;


    switch(lParam)
    {
    case 4:
    m_button_cancel.ModifyStyle(WS_VISIBLE,WS_DISABLED,0);
    m_button_download.ModifyStyle(WS_DISABLED,WS_VISIBLE,0);
    CFoxUpdateDlg::RedrawWindow();
    s_status_out = "Total " ;
    s_status_out = s_status_out + s_downloaded_file_len + " bytes finished!" ;
    m_connect_status.Format("%s",  s_status_out); 
    UpdateData(FALSE);
    break;
    case 1:
    m_connect_status = "File size checking...";
    UpdateData(FALSE);
    break;
    default:
    s_status_out = "Now, ";
    s_status_out = s_status_out + s_downloaded_file_len + " of "+s_file_len+" Bytes downloaded...";
    m_connect_status.Format("%s",  s_status_out); 

    float db_speed = 0;
    if(nowtime == l_begin)
    {
    m_Speed.Format("%s",">1M");
    }
    else
    {
    db_speed = (float)n_downloaded_file_len /(float)(nowtime - l_begin);
    m_Speed.Format("%9.2f", db_speed);
    }
    UpdateData(FALSE);
    }
    }
    else
    {
    DeleteFile("temp.bin");
    m_button_cancel.ModifyStyle(WS_VISIBLE,WS_DISABLED,0);
    m_button_download.ModifyStyle(WS_DISABLED,WS_VISIBLE,0);
    CFoxUpdateDlg::RedrawWindow();

    switch(lParam)
    {
    case 8:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Error Bin files!");
    UpdateData(FALSE);

    CFoxUpdateDlg::getpassword(); 
    break;
    case 7:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Windows error,restart recommended!");
    UpdateData(FALSE);
    break;
    case 6:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Write file error,restart recommended!");
    UpdateData(FALSE);
    break;
    case 5:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Can't connect with remote server!");
    UpdateData(FALSE);
    break;
    case 4:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Criticle error!check cable!");
    UpdateData(FALSE);
    break;
    case 3:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Internet authenticates rejected!");
    UpdateData(FALSE);
    CFoxUpdateDlg::getpassword(); 
    break;
    case 2:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Connect failed!");
    UpdateData(FALSE);
    break;
    case 1:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Remote server error!");
    UpdateData(FALSE);
    break;
    default:
    m_Speed.Format("%s","0.0");
    m_connect_status.Format("%s","Cancel by user!");
    UpdateData(FALSE);
    }
    }}void CFoxUpdateDlg::OnCancel() 
    {
    // TODO: Add extra cleanup here
    if(!b_cancel)
    b_cancel = TRUE; 
    }void CFoxUpdateDlg::getpassword()
    {

    CPasswordDlg pwd_dlg;
    //CFoxUpdateDlg::ShowWindow(SW_HIDE); 
    pwd_dlg.DoModal();
    if(!b_cancel)
    {
    //MessageBox(login_name + "\n" + pwd);
    m_connect_status = "password!ok";
    UpdateData(FALSE);
    CFoxUpdateDlg::Ondownload(); 
    }
    else
    {
    m_connect_status = "User canceld!";
    UpdateData(FALSE);
    }
    }void CPasswordDlg::OnOK() 
    {
    // TODO: Add extra validation here
    UpdateData(TRUE);
    strcpy(pwd , m_password.GetBuffer(30));
    strcpy(login_name , m_loginname.GetBuffer(30));
    CDialog::OnOK();
    }void CPasswordDlg::OnCancel() 
    {
    // TODO: Add extra cleanup here
    b_cancel = TRUE;
    CDialog::OnCancel();
    }
      

  4.   

    唉!
    自己解决了……只是没有文档的api