bonjour,
j'aimerai savoir comment recuperer le contenu d'une page web via son url avec visual c++
merci
Version imprimable
bonjour,
j'aimerai savoir comment recuperer le contenu d'une page web via son url avec visual c++
merci
salut,
Ton appli est de quel type? (MFC ou ATL Server)
De quel contenu parles-tu? S'agit-il des variables d'un formulaire ou des paramètres d'une URL?
Avec ATL Server tu utilises l'objet m_HttpRequest dans ta méthode ValidateAndExchange() de ton gestionnaire de requête pour obtenir soit:
1. Des variables d'un formulaire d'une page Web avec
qui retourne un objet collection des variables du formulaire ou soitCode:const CHttpRequestParams& CHttpRequest::GetFormVars() const throw( );
2. Des paramètres d'url du genre "http://www.myserver.com/page.srf?param1=value1¶m2=value2" avec
qui retourne un objet collection des paramètres de l'url.Code:const CHttpRequestParams& CHttpRequest::GetQueryParams() const throw( );
Slt et merci d'avoir pris le temps de commencer à me répondre,
en fait, je recois visual c++ cette semaine, c'est en vu de ca que je commence à me rencarder sur le how to recuperer une page net via c++,
ben mon appli (MFC ou ATL Server : connais pas ca ! :() sera une appli windows classique, fenetré
je dois recuperer une page web, l'analyser et aller recuperer une autre page web avec des parametre
ex :
recup de www.monsite.com/pagearecup1.php
----> analyse
recup de www.monsite.com/pagearecup2.php.param1=xxx¶m2=yyy
Quel genre d'analyse ?
Une fonction simple pour télécharger c'est URLDownloadToFile.
ben en fait,
la page est formater genre csv,
il s'agit d'une lecture de la base de donnée champs par champs,
ex :
www.monsite.com/?login=toto&mdp=123456
si le login est ok, je recois un page telle que :
NOM1;TEL1
NOM2;TEL2
NOM3;TEL3
etc
mon but est de recuperer ces data, pour ensuite travailler dessus en c++
je ne comprend pas trop bien. :?
Quelqu'un se logue sur ton site et tu veux récupérer des infos?
Comment comptes-tu définir ton application?
Si tu veux afficher une page web tu as les apis de wininet ou les classes MFC de WinInet. En plus dans une application MFC tu as la classe CHtmlView, tu as aussi les classes ISAPI pour cela.
Coté ATL Server c'est comme je t'ai dit.
Pour un développement classique tu dois directement manipuler les apis.
C'est mieux d'utiliser des librairies toutes faites pour le cas.
Jsuis vraiment pas bon pour expliquer ce que je dois faire apparemment,
donc je reprend en + clair (j'espere:))
donc :
mon application sous windows recupere une page web par son url, cette page necessite un login et un mot de passe mais ca, c'est dans l'url.
donc en gros, c'est comme si je voulais recuperer le code html de www.google.com; ce qui me donnerait :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><style><!-- body,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .q{color:#0000cc;} //--> </style> <script> <!-- function sf(){document.f.q.focus();} function rwt(el,ct,cd,sg){el.href="/url?sa=t&ct="+escape(ct)+"&cd="+escape(cd)+"&url="+escape(el.href).replace(/\+/g,"%2B")+"&ei=J1RCQ7rmPIHeQvvqtP8G"+sg;el.onmousedown="";return true;} // --> </script> </head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onLoad=sf() topmargin=3 marginheight=3><center><img src="/intl/fr_fr/images/logo.gif" width=276 height=110 alt="Google"><br><br> <form action=/search name=f><script><!-- function qs(el) {if (window.RegExp && window.encodeURIComponent) {var ue=el.href;var qe=encodeURIComponent(document.f.q.value);if(ue.indexOf("q=")!=-1){el.href=ue.replace(new RegExp("q=[^&$]*"),"q="+qe);}else{el.href=ue+"&q="+qe;}}return 1;} // --> </script><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>Web</b> <a id=1a class=q href="/imghp?hl=fr&tab=wi" onClick="return qs(this);">Images</a> <a id=2a class=q href="http://groups.google.fr/grphp?hl=fr&tab=wg" onClick="return qs(this);">Groupes</a> <a id=3a class=q href="/dirhp?hl=fr&tab=wd" onClick="return qs(this);">Annuaire</a> <a id=4a class=q href="http://news.google.fr/nwshp?hl=fr&tab=wn" onClick="return qs(this);">Actualités</a> <b><a href="/intl/fr/options/" class=q>plus »</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%> </td><td align=center><input type=hidden name=hl value=fr><input maxLength=256 size=55 name=q value=""><br><input type=submit value="Recherche Google" name=btnG><input type=submit value="J'ai de la chance" name=btnI></td><td valign=top nowrap width=25%><font size=-2> <a href=/advanced_search?hl=fr>Recherche avancée</a><br> <a href=/preferences?hl=fr>Préférences</a><br> <a href=/language_tools?hl=fr>Outils linguistiques</a></font></td></tr><tr><td colspan=3 align=center><font size=-1>Rechercher dans : <input id=all type=radio name=meta value="" checked><label for=all> Web</label><input id=lgr type=radio name=meta value="lr=lang_fr" ><label for=lgr> Pages francophones</label><input id=cty type=radio name=meta value="cr=countryFR" ><label for=cty>Pages : France</label></font></td></tr></table></form><br><br><font size=-1><a href=/ads/>Publicité</a> - <a href=/services/>Solutions d'entreprise</a> - <a href=/intl/fr/about.html>À propos de Google</a> - <a href=http://www.google.com/ncr>Google.com in English</a></font><p><font size=-2>©2005 Google</font></p></center></body></html>
apres, je ferai un traitement de chaine sur ce que j'ai recuperé.
c'est tout :)
plus personne ?
:(
t'as regardé du coté de WinINet et de HttpOpenRequest et de ses ptits frères ?
connais pas ....
jvais aller voir ca,
merci
:)
enfin ... si je puis me permettre un conseil, si tu utilises les MFC, il faut privilégier les CInternetSession ou autres CHttpConnection
C'est vraiment vraiment vraiment vraiment plus simple :wink:
tu as trouvé ta solution ?
sinon, j'ai fait ca entre temps
ca devrait marcher :à la fin, s contient le contenu de la pageCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #include <Wininet.h> #include <string> using namespace std; #pragma comment (lib, "Wininet.lib") #define TAILLE_BUFFER 256 HINTERNET hInternetSession, hHttpSession; hInternetSession = InternetOpen ("mon app", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL) ; if (!hInternetSession) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (const char *)lpMsgBuf, "", MB_ICONSTOP); LocalFree( lpMsgBuf ); return 0; } hHttpSession = InternetOpenUrl( hInternetSession, "http://www.google.fr", NULL, 0, 0, 0 ) ; if (!hHttpSession) { InternetCloseHandle (hInternetSession) ; LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (const char *)lpMsgBuf, "", MB_ICONSTOP); LocalFree( lpMsgBuf ); return 0; } char buffer[TAILLE_BUFFER]; DWORD nb = -1; string s; DWORD dwRead; while ( InternetReadFile( hHttpSession, buffer, TAILLE_BUFFER-1, &dwRead ) ) { if ( dwRead == 0 ) break; buffer[dwRead] = 0; s += buffer; } InternetCloseHandle( hHttpSession ); InternetCloseHandle (hInternetSession) ;
Je te remercis bien pour ton bout de code ... :)
en fait, j'attend toujours impatiemment le livreur,
VC++ n'est toujours pas arrivé ! :(
Si tu passes par un fichier:
Code:
1
2
3
4
5 URLDownloadToFile( NULL, "http://www.google.fr", "C:\\temp.html", NULL );
non, ca par contre,
ce sera en mémoire, pas de cache
je sais pas comment ca s'appelle en C++, mais en delphi
j'aurai utilisé un TMemoryStream
j'imagine que ca existe aussi en C/C++
j'ai de gros espoirs sur le code de nico-pyright(c) :)
L'équivalent serait peut être un stringstream, mais là nico utilise une string, et ça convient très bien (elle est remplie progressivement par réception de petits bouts de taille TAILLE_BUFFER).
:) cool alors :)
merci bien les gars,
des que je recois mon pack, je validerai "Résolu" pour ce post
(à moins qu'il ne faille que je le mette maintenant pour le reouvrir eventuellement)
Re-Slt,
ca y est, j'ai recu mon visual c++,
mais la ... on dirait vraiment un bouricot !
jsuis un habitué de delphi qui avait débuté sur Visual Basic, du coup, la, jsuis un peu perdu,
j'ai fait un nouveau projet winform,
j'ai essayé de coller ce code dans un bouton
j'ai 102 erreurs dans la lib WinInet !!!!Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 #include <Wininet.h> #include <string> #pragma once #pragma comment (lib, "Wininet.lib") #define TAILLE_BUFFER 256 namespace declic { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::Button * button1; private: System::Windows::Forms::TextBox * textBox1; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container * components; /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->button1 = new System::Windows::Forms::Button(); this->textBox1 = new System::Windows::Forms::TextBox(); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(16, 16); this->button1->Name = S"button1"; this->button1->TabIndex = 0; this->button1->Text = S"button1"; this->button1->Click += new System::EventHandler(this, button1_Click); // // textBox1 // this->textBox1->Location = System::Drawing::Point(120, 16); this->textBox1->Multiline = true; this->textBox1->Name = S"textBox1"; this->textBox1->Size = System::Drawing::Size(264, 176); this->textBox1->TabIndex = 1; this->textBox1->Text = S""; // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(528, 273); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Name = S"Form1"; this->Text = S"Form1"; this->ResumeLayout(false); } private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { /* String *strCat; String *something = textBox1->Text; String *somethingElse = "test" ; strCat = strCat->Concat(something," ",somethingElse); textBox1->Text = strCat;*/ HINTERNET hInternetSession, hHttpSession; hInternetSession = InternetOpen ("mon app", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL) ; if (!hInternetSession) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (const char *)lpMsgBuf, "", MB_ICONSTOP); LocalFree( lpMsgBuf ); return 0; } hHttpSession = InternetOpenUrl( hInternetSession, "http://www.google.fr", NULL, 0, 0, 0 ) ; if (!hHttpSession) { InternetCloseHandle (hInternetSession) ; LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (const char *)lpMsgBuf, "", MB_ICONSTOP); LocalFree( lpMsgBuf ); return 0; } char buffer[TAILLE_BUFFER]; DWORD nb = -1; string s; DWORD dwRead; while ( InternetReadFile( hHttpSession, buffer, TAILLE_BUFFER-1, &dwRead ) ) { if ( dwRead == 0 ) break; buffer[dwRead] = 0; s += buffer; } InternetCloseHandle( hHttpSession ); InternetCloseHandle (hInternetSession) ; }}; }
à commencer par :
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinInet.h(56) : error C2146: syntax error : missing ';' before identifier 'HINTERNET'
ca peut venir d'ou à votre avis (si ce n'est mon incompétence) ?
Faut d'abord inclure <windows.h> dans un projet Win32.
Note que là tu as créé un projet .Net.