[Visual C++] Connexion à ne URL

Connexion à ne URL [Visual C++] - Programmation

Marsh Posté le 31-03-2001 à 09:24:04    

Une question de petit nouveau en prog. Windows : comment faire ne visual C++ pour se connecter à une URL et ramener une page ?
 
Thx

Reply

Marsh Posté le 31-03-2001 à 09:24:04   

Reply

Marsh Posté le 31-03-2001 à 09:51:56    

Salut,
 
Dans (imaginons une application boite de dialogue)ta ressource de boite de dialogue, tu cliques bouton droit et fais "insère active X control".  Là tu cherches "Navigateur microsoft" et tu l'insères.
Ensuite tu crées une variable qui y fait référence, par exemple m_Browser.
Ensuite dans ton code, tu insères pour naviguer : m_Browser.Navigate("url", NULL, NULL, NULL, NULL);
 
Voilà!
Conculte la doc pour les autres fonctions membres et les paramètres.
 
@+
Flying

Reply

Marsh Posté le 31-03-2001 à 12:24:54    

Si tu veux pouvoir lire le contenu de la page (pour l'analyser par exemple), tu peux utiliser l'ensemble des classes de connections : CHttpFile, CHttpConnection et CInternetSession.  
 
Il y a toute une serie d'exemple dans la doc.
 
Hope this help


---------------
Pipiru piru piru pipiru pi
Reply

Marsh Posté le 01-04-2001 à 11:17:34    

Il y a le programme TEAR.CPP qui explique bien comment récupérer le contenu d'une page internet.
Je m'en suis servi pour des essais.
 
Sinon avec le contrôle WebBrowser il y a aussi moyen de récupérer le contenu HTML.  J'ai fait une fonction qui le transfère dans un CString.  Si tu veux je le post ici.
 
@+
Flying

Reply

Marsh Posté le 01-04-2001 à 19:41:34    

Une méthode basique:
telnet www.nom-du-site.com 80
(ne pas mettre http:// devant www)
puis taper (sans faire de faute de frappe et sans corriger, car le protocole HTTP n'est pas interactif et ne sait pas le faire):
 
GET /chemin/page.html HTTP/1.1
Host: www.nom-du-site.com  
 
(ligne vide au dessus)
 
En réponse tu as
 
200 OK HTTP/1.1
si la requête était correcte
suivi des entêtes MIME terminés par un double saut de ligne, et suivi du corps du document demandé, puis la session se termine.

Reply

Marsh Posté le 01-04-2001 à 22:46:39    

y'a une méthode toute prête dans le internet sdk... tu files l'url et il te ramene ce qu'il y a à l'autre bout....
je te la file demain du boulot

Reply

Marsh Posté le 01-04-2001 à 23:01:52    

Méthode avec WebBrowser control et MFC :
 
//////////////////////////////////////////////////////////////////////
// Transfère dans une variable CSTRING le contenu de la page Web //
//////////////////////////////////////////////////////////////////////
 
CString* CNM_CLIENTDlg::GetHTML()
{
 IUnknown* pUnk;
 LPDISPATCH lpDisp;
 HRESULT hr;
 IHTMLDocument2* pDocument;
 IDispatch* pDispatch;
 IHTMLElementCollection* pCollection;
 
 m_strHTML = "";
 pUnk = m_Browser.GetDocument();  // Get document
 if (pUnk == NULL) return &m_strHTML; // Echec
 
 hr = pUnk->QueryInterface(IID_IDispatch, (void**)&lpDisp);
 if (hr != S_OK) return &m_strHTML;  // Echec
 
 hr = lpDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument);
 
 if ((S_OK == pDocument->get_all(&pCollection)) &&  
  (NULL != pDocument) &&  
  (S_OK == pCollection->tags(_variant_t("HTML" ), &pDispatch))
  && (NULL != pDispatch))
  {
   IHTMLElementCollectionPtr pCollectionHTML = pDispatch;
   if ( pCollectionHTML != NULL )
   {
    IDispatch* pDispatchHTML;
    if ( S_OK == pCollectionHTML->item(_variant_t(0L), _variant_t(), &pDispatchHTML))
    {
     IHTMLElementPtr pHTML = pDispatchHTML;
     if (NULL != pHTML)
     {
      BSTR bstrHTML = NULL;
      if (S_OK == pHTML->get_outerHTML(&bstrHTML))
      {
       m_strHTML = bstrHTML;  // Convertit en CString
       ::SysFreeString(bstrHTML);
       pHTML->Release();
      }
     }
    }
   }
   pCollectionHTML->Release();
  }
 pCollection->Release();
 pDocument->Release();
 lpDisp->Release();
 pUnk->Release();
 return &m_strHTML;
}
 
Flying

Reply

Marsh Posté le 01-04-2001 à 23:13:41    

Voilà encore une autre méthode en utilisant les sockets au lieu du controle activeX WebBrowser.
J'ai pas mis les #include ... si quelqu'un en a besoin, je les poste.
 
LPCTSTR pszURL;
CSession session(_T("Nom de la session" ), dwAccessType);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strServerName, strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
DWORD dwRet;
 
if (!AfxParseURL(pszURL, dwServiceType, strServerName, strObject, nPort) ||
 (dwServiceType != INTERNET_SERVICE_HTTP))
{
 // Parsing incorrect
}
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
  strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
 
// Transfert ok ?
pFile->QueryInfoStatusCode(dwRet);
if (dwRet != 200)
{
 // Erreur
}
 
// Transfère le fichier lu dans un CString
CString m_strHtml;
CString CR = "\r\n";
CString strbuffer = "";
while (pFile->ReadString(strbuffer))
 m_strHtml = m_strHtml + strbuffer + CR;
 
pServer->Close();
pFile->Close();
session.Close();
 
__________________
 
Oups...oublié
Définir une classe :
CSession::CSession(LPCTSTR pszAppName, int nMethod)
: CInternetSession(pszAppName, 1, nMethod)
{
}
 
et les variables ci-dessous :
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
DWORD dwHttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT |  
INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_RELOAD;
TCHAR szHeaders[] = "";

 

[edit]--Message édité par Flying--[/edit]

Reply

Marsh Posté le 02-04-2001 à 09:51:54    

Merci bcp pour toutes ces précieuses infos... J'ai pas encore testé !

Reply

Marsh Posté le 02-04-2001 à 12:59:28    

URLDownloadToFile(NULL,url_char,name_to_save_char,0,NULL))
avec
#include <urlmon.h>
 
Downloads bits from the Internet and saves them to a file.
 
Syntax
 
HRESULT URLDownloadToFile(
    LPUNKNOWN pCaller,
    LPCTSTR szURL,
    LPCTSTR szFileName,
    DWORD dwReserved,
    LPBINDSTATUSCALLBACK lpfnCB
);
 
Parameters
 
pCaller  
Address of the controlling IUnknown interface of the calling ActiveX® component (if the caller is an ActiveX component). If the calling application is not an ActiveX component, this value can be set to NULL. Otherwise, the caller is a COM object that is contained in another component (such as an ActiveX control within the context of an HTML page). This parameter represents the outermost IUnknown of the calling component. The function attempts the download within the context of the ActiveX client framework and allows the caller's container to receive callbacks on the progress of the download.  
szURL  
Address of a string value containing the URL to be downloaded. This cannot be set to NULL.  
szFileName  
Addrss of a string value containing the name of the file to create for bits that come from the download.  
dwReserved  
Reserved for future use. Must be zero.  
lpfnCB  
Address of the caller's IBindStatusCallback interface. URLDownloadToFile calls this interface's IBindStatusCallback::OnProgress method on a connection activity, including the arrival of data. IBindStatusCallback::OnDataAvailable is never called. Implementing IBindStatusCallback::OnProgress allows a caller to implement a user interface or other progress monitoring functionality. It also allows the download operation to be canceled by returning E_ABORT from the IBindStatusCallback::OnProgress call. This can be set to NULL.  
Return Value
 
Returns one of the following values:
 
E_OUTOFMEMORY The buffer length is invalid or there was insufficient memory to complete the operation.  
S_OK The operation succeeded.  
 
Remarks
 
The client can choose to be notified of progress through a notification callback.
 
Function Information
 
Windows NT Use version 4.0. Implemented as ANSI and Unicode functions.  
Windows Use Windows 95 and later. Implemented as ANSI and Unicode functions.  
Header Urlmon.h  
Import Library Urlmon.lib  
Minimum availability Internet Explorer 3.0

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed