我用WINSCOK去获得HTML的代码,有的可以有的得到的结果是 301 moved permanently的?但我在IE里面却都能打开的?怎么才可以象IE那样一定会获得这个网页的呢? 
我的获取代码如下:
CInternetSession internetSession;
//-----------------------------
//10秒后time out
DWORD dwTimeout = 60000;
internetSession.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT,dwTimeout);
    //-----------------------------
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
DWORD dwServiceType = 0;
DWORD dwHttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
TCHAR szHeaders[] = _T("Accept: text/*\r\nUser-Agent: LCD's Sample Http Client\r\n");
INTERNET_PORT nPort;
int nRet = 0;
try//异常捕捉
{
nRet = AfxParseURL( (LPCTSTR)sURL, dwServiceType, strServer, strObject, nPort );
if( !nRet || dwServiceType != INTERNET_SERVICE_HTTP )
{
fileContent.Empty();
//异常捕捉
OnwirteReport("B1",str_website,sURL,"不能保存请求保存网页的源代码");
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}
//
  
pHttpConnection = internetSession.GetHttpConnection( strServer, nPort );
m_param.pt_OutTwo->SetWindowText("开始提交http关于"+sURL+"的请求");
pHttpFile = pHttpConnection->OpenRequest( CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
if(!pHttpFile->AddRequestHeaders( szHeaders ))
{
     fileContent.Empty();
//异常捕捉
OnwirteReport("B1",str_website,sURL,"无法获得http报头");
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}
  }
     catch(CInternetException* ex)
{

fileContent.Empty();
OnwirteReport("B5",str_website,sURL,"无法发送http报头,可能网络状况有问题");
delete ex;
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}
m_param.pt_OutTwo->SetWindowText("提交http关于"+sURL+"的请求完毕");
//
try//异常捕捉--------------------------------------------------------------
{
if(!pHttpFile->SendRequest())
{
fileContent.Empty();

OnwirteReport("B5",str_website,sURL,"无法发送http报头,可能网络状况有问题");
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}
}
catch(CInternetException* ex)
{

fileContent.Empty();
OnwirteReport("B5",str_website,sURL,"无法发送http报头,可能网络状况有问题");
delete ex;
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}
//
//}//RunTime 错误所在fixed------------------------------------------------------------
   DWORD dwRet = 0;
try//异常捕捉--------------------------------------------------------------
{
pHttpFile->QueryInfoStatusCode( dwRet ); if( dwRet == HTTP_STATUS_DENIED )
{
fileContent.Empty();
//异常捕捉
OnwirteReport("B1",str_website,sURL,"连接被拒绝");
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
} CString string, prefix, suffix, path, sCopy, sTemp, strFilePath;
int     iStart = 0, iEnd = 0;

strFilePath = sURL.Left( ( sURL.ReverseFind( '/' ) ) );
fileContent.Empty();
while( pHttpFile->ReadString( string ) )
{
fileContent += "\r\n" + string;
}
pHttpFile->Close();
pHttpConnection->Close();
delete pHttpFile;
delete pHttpConnection;
}
catch(CInternetException* ex)
{
         fileContent.Empty();
OnwirteReport("B5",str_website,sURL,"无法发送http报头,可能网络状况有问题");
delete ex;
//test code
       OnPutOutFunction("GetSourceByURL运行出错",test_time);
       //
return FALSE;
}

解决方案 »

  1.   

    //initalization internet Dll,this is a callback function
    HINTERNET hSession = ::InternetOpen(
    "Raw HTML Reader",
    PRE_CONFIG_INTERNET_ACCESS,
    "",
    INTERNET_INVALID_PORT_NUMBER,
    0);
    if(hSession==NULL)
    {
    AfxMessageBox("Internet session initalization failed!");
    return 0;
    }
    //initaliaztion HTTP session
    HINTERNET hConnect = ::InternetConnect(
    hSession,
    m_strServer,
    INTERNET_INVALID_PORT_NUMBER,
    "",//user name
    "",//pass word
    INTERNET_SERVICE_HTTP,
    0,
    0);
    //verify this handle 
    if(hConnect==NULL)
    {
    AfxMessageBox("Internet connect inilization failed!");
    //close session HANDLE
    VERIFY(::InternetCloseHandle(hSession));
    return 0;
    }
    //3: open a HTTP requration
    HINTERNET hHttpFle = ::HttpOpenRequest(
    hConnect,
    "GET",
    m_strPath,
    HTTP_VERSION,
    NULL,
    0,
    INTERNET_FLAG_DONT_CACHE,
    0);
    // verify connect handle is valid.
    // verify session handle is valid.
    if(hHttpFle==NULL)
    {
    AfxMessageBox("Http request failed!");
    VERIFY(::InternetCloseHandle(hConnect));
    VERIFY(::InternetCloseHandle(hSession));
    return 0;
    }
    // show wait cursor
    CWaitCursor wait;
    //ShowCursor(TRUE);
    //send reauest:
    BOOL bSendRequset = ::HttpSendRequest(
    hHttpFle,
    NULL,
    0,
    0,
    0);
    if(bSendRequset)
    {
    //get file size:
    char achQueryBuf[32];
    DWORD dwFileSize(0);
    DWORD   dwQueryBufLen=sizeof(achQueryBuf);
    BOOL bQuery = ::HttpQueryInfo(
    hHttpFle,
    HTTP_QUERY_CONTENT_LENGTH,
    achQueryBuf,
    &dwQueryBufLen,
    NULL);
    if(bQuery)
    {
    dwFileSize = (DWORD)atol(achQueryBuf);
    }
    else
    {
    dwFileSize = 5*1024;
    }
    //allocate a buff for file data.
    lpszBuf=NULL;
    lpszBuf=new char[dwFileSize+1]; //read file
    DWORD dwBytesRead=0;
    BOOL bRead = ::InternetReadFile(
    hHttpFle,
    lpszBuf,
    dwFileSize+1,
    &dwBytesRead);
    if(bRead)
    {
    lpszBuf[dwBytesRead]=0;
    }
    //close INTERNET HANDLE
    VERIFY(::InternetCloseHandle(hHttpFle));
    VERIFY(::InternetCloseHandle(hConnect));
    VERIFY(::InternetCloseHandle(hSession));
    PostMessage(m_hPostMsgWnd,WM_READFILECOMPLETED,NULL,NULL);
    }