c++ stiahnutie suboru/nacitanie stranky

Programovacie jazyky, rady, poradňa...
caesar1987
Addict
Addict
Používateľov profilový obrázok
Príspevky: 3001
Registrovaný: 02 okt 2005, 0:57
Bydlisko: Nové Zámky
Kontaktovať používateľa:

c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa caesar1987 »

Ahojte

potrebujem vo visual c++ 2008 stiahnut subor z netu alebo nacitat webovu stranku do premennej

viete mi dat niekto nejaky priklad ale nie Curl (ten mi bohuzial nefunguje nech ho skompilujem ja alebo pouzijem skompilovany)

Na nete som hladal ale nic funkcne som nenasiel.

Dakujem za pomoc
c-ice
Medium Star
Medium Star
Používateľov profilový obrázok
Príspevky: 475
Registrovaný: 04 mar 2008, 15:18
Kontaktovať používateľa:

Re: c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa c-ice »

mozez pouzit pomocou winsock nadviazat spojenie a prijat data ked to ma byt iba stranka myslim ze by malo ist aj MIME takto nejak prijat

alebo skusit Qt ktore ma na to prepracovane triedy kde takmer nic nemusis nastavovat
caesar1987
Addict
Addict
Používateľov profilový obrázok
Príspevky: 3001
Registrovaný: 02 okt 2005, 0:57
Bydlisko: Nové Zámky
Kontaktovať používateľa:

Re: c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa caesar1987 »

vies mi dat aj nejake priklady?

//edit pozeral som to qt a nemam sajnu ako ho dostat do visual studia

nejake dalsie poznosti?
c-ice
Medium Star
Medium Star
Používateľov profilový obrázok
Príspevky: 475
Registrovaný: 04 mar 2008, 15:18
Kontaktovať používateľa:

Re: c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa c-ice »

Toto som pouzil ked som robil taky program poqec klient vid programatorsky kutik nie je to pôvodne samozrejme som to upravil povodny si mozes vygooglit kod...

keby viac hladas tak najdes nejaky Qt plugin pre visual studio resp. preco musi byt striktne Visual studio ked mozes pouzit "Qt developera"

Kód: Vybrať všetko

/*
*                                 http_post.cpp 
*
* by Uday Chitragar - 2004/Dec/01
* 
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must
* not claim that you wrote the original software. If you use this
* software in a product, an acknowledgment in the product documentation
* would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
* 
* */

/* 
* Notes:
* This source demonstrates sending HTTP POST request to webserver from C++
* This uses sockets hence can be compiled on Linux, UNIX, Win
*/


//For commn
#include "StdAfx.h"
#include "MyTray.h"
#include <Winsock2.h>
#include "http_post.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Chttp_post

Chttp_post::Chttp_post()
{
}

Chttp_post::~Chttp_post()
{
}


// Do not edit the following lines, which are needed by ClassWizard.

BEGIN_MESSAGE_MAP(Chttp_post, CDialog)
	//{{AFX_MSG_MAP(Chttp_post)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Chttp_post member functions

#define SEND_RQ(MSG) \
	/*cout<<send_str;*/ \
	send(sock,MSG,strlen(MSG),0);



//<exe> hostname api parameters
//	api = napr. Request URI: /prihlasenie.phtml?KDE=wap.azet.sk/index.phtml~&WAP=1
//	hostname= www.... aj http ?
//	loginForm=nick=c-ice&heslo="tu je tajne heslo"&PosliForm=Prihl%C3%A1si%C5%A5
//	message= 200 ?
CString Chttp_post::requestEx (struct structRequest strcReq) 
{

	WSADATA	WsaData;
	WSAStartup (0x0101, &WsaData);


	sockaddr_in       sin;
	int sock = socket (AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		return "-100";
	}
	sin.sin_family = AF_INET;
	sin.sin_port = htons( (unsigned short)80);

	struct hostent * host_addr = gethostbyname(strcReq.sockHostName);
	if(host_addr==NULL) {
		AfxMessageBox( "Unable to locate host");
		return "-103";
	}
	sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
	//AfxMessageBox("Port :%s , Address :%s ",sin.sin_port,sin.sin_addr.s_addr);

	if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
		AfxMessageBox("connect failed") ;
		return "-101";
	}

	CString send_str;

	SEND_RQ(strcReq.strOptional);
	SEND_RQ(strcReq.strURIRequestApi);
	SEND_RQ(strcReq.strVersion);
	SEND_RQ(strcReq.strHost);
	SEND_RQ(strcReq.strUserAgent);
	SEND_RQ(strcReq.strAcceptFiles);

	char content_header[100];
	sprintf(content_header,"Content-Length: %i\r\n",strlen(strcReq.strTextData));

	SEND_RQ(strcReq.strAcceptLang);
	SEND_RQ(strcReq.strAcceptEncoding);
	SEND_RQ(strcReq.strAcceptCharset);
	SEND_RQ(strcReq.strKeepLive);
	SEND_RQ(strcReq.strConnection);
	if (strcReq.strReferer!="")
		SEND_RQ(strcReq.strReferer);
	if (strcReq.strContentType!="")
		SEND_RQ(strcReq.strContentType);
	if (strcReq.strTextData!="")
		SEND_RQ(content_header);
	SEND_RQ("\r\n");
	if (strcReq.strTextData!="")
		SEND_RQ(strcReq.strTextData);

	//AfxMessageBox("####HEADER####");
	char c1[1];
	CString message;
	int l,line_length;
	bool loop = true;
	bool bFound = false;
	bool bOk = false;
	bool bRecv = false;
	int iLocationTest=-1,iEndTest=-1,iRPEnd=-1,iRPEndTest=0;
	do{
		loop=true;
		iRPEnd=message.GetLength();
		while(loop) {
			l = recv(sock, c1, 1, 0);
			if(l<0) loop = false;
			if(message.Find("</html>") != -1)loop=false;
			if(c1[0]=='\n') {
				if(line_length == 0) loop = false;

				line_length = 0;
				if(message.Find("302") != -1)
					bFound = true;
				if(message.Find("200 OK") != -1)
					bOk = true;
			}
			else if(c1[0]!='\r') line_length++;
			//_DEBUG_PRINT( cout<<c1[0]);
			message += c1[0];
		}
		iLocationTest=message.Find("Location: ");
		iEndTest=message.Find("</html>");
		//if(strcReq.strOptional.Find("GET")!=-1)iRPEnd=-1;
		//	else iRPEnd=strcReq.sockHostName.Find("rpx.azet.sk");//aby sa cyklus ukoncil pri odoslani RP :)
		iRPEndTest=message.GetLength()-1;
	}while((iLocationTest==-1)&&(iEndTest==-1)&&(iRPEnd!=iRPEndTest)&&(bFound!=true));
	WSACleanup( );
	return message;
}



Kód: Vybrať všetko

// http_post.h : header file
//
#if !defined(AFX_HTTP_POST_H__91365852_B4AF_4381_B1AB_35454719C9E8__INCLUDED_)
#define AFX_HTTP_POST_H__91365852_B4AF_4381_B1AB_35454719C9E8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


/////////////////////////////////////////////////////////////////////////////
// Chttp_post command target

class Chttp_post : public CDialog
{
// Construction
public:
	Chttp_post();
// Attributes
public:

	struct structRequest{
		CString sockHostName; //napr. "moje.azet.sk"
		CString strOptional;//="POST";	//or GET or PUT
		CString strURIRequestApi;	//Request URI= "/prihlasenie.phtml?KDE=wap.azet.sk/index.phtml~&WAP=1"
		CString strVersion;//=" HTTP/1.1\r\n";
		CString strHost;	// "Host: moje.azet.sk\r\n"
		CString strUserAgent;//="User-Agent: Mozilla/4.0\r\n";
		CString strAcceptFiles;//="Accept: text/html\r\n";
		CString strAcceptLang;//="Accept-Language: sk,cs;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
		CString strAcceptEncoding;//="Accept-Encoding: gzip, deflate\r\n";
		CString strAcceptCharset;//="Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n";
		CString strKeepLive;//="Keep-Alive: 300\r\n";
		CString strConnection;//="Connection: keep-alive\r\n";
		CString strReferer;			//"Referer: http://moje.azet.sk/prihlasenie.phtml?&WAP=1&KDE=wap.azet.sk/index.phtml~\r\n"
		CString strContentType;//="Content-Type: application/x-www-form-urlencoded\r\n";
		//CString strContentLenght;	//"Content-Length: %i\r\n",strlen(loginForm)
		CString strTextData;		//loginForm="nick=c-ice&heslo="tu je tajne heslo"&PosliForm=Prihl%C3%A1si%C5%A5"
	} structRequest;
// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(Chttp_post)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~Chttp_post();

	// Generated message map functions
public:
	
	//{{AFX_MSG(Chttp_post)
		static CString requestEx (struct structRequest strcReq); //structure to get all data check http_post.h
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_HTTP_POST_H__91365852_B4AF_4381_B1AB_35454719C9E8__INCLUDED_)
martasko
Amateur
Amateur
Príspevky: 10
Registrovaný: 19 dec 2006, 17:07

Re: c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa martasko »

zo stranok QT si stiahni:
Qt libraries 4.6.3 for Windows (VS 2008, 194 MB)
a potom este QT addin pre Visual studio:
Visual Studio Add-in (44 MB)
Pokial viem, tak je len podpora pre VS 2008 pre VS 2010 nie je.
Taktiez mozes vyuzit BOOST sockety ale pod windows som s nimi nikdy nerobil, tak neviem poradit.
caesar1987
Addict
Addict
Používateľov profilový obrázok
Príspevky: 3001
Registrovaný: 02 okt 2005, 0:57
Bydlisko: Nové Zámky
Kontaktovať používateľa:

Re: c++ stiahnutie suboru/nacitanie stranky

Príspevok od používateľa caesar1987 »

nakoniec som pouzil toto

Kód: Vybrať všetko

...
using namespace System;
using namespace std;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;

...

string get_page(String ^loginurl){

	HttpWebRequest^ myReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create(loginurl));
	WebResponse ^ WebResponse = myReq->GetResponse();
	System::IO::Stream ^WebStream = WebResponse->GetResponseStream();
	Encoding^ encode = System::Text::Encoding::GetEncoding( "utf-8" );
	StreamReader^ readStream = gcnew StreamReader( WebStream,encode );
	array<Char>^ read = gcnew array<Char>(256);
	int count = readStream->Read( read, 0, 256 );

	string out = "";
	while ( count > 0 ){
		// Dumps the 256 characters on a String* and displays the String* to the console.
		String^ str = gcnew String( read,0,count );
		MessageBox::Show(str);
		out.append(systemToStd(str));
		count = readStream->Read( read, 0, 256 );
	}

	// Cleanup
	WebResponse->Close();
	readStream->Close();

	return out;


}

bez problemov ide aj v release mode vo visual c++ 2008

a visual studio preto lebo (vacsi) projekt/program je napisany v nom a pouziva jeho kniznice.
Napísať odpoveď