Clarification: string obtenir à partir de l'URL
- manal
- Born


- Inscription: Oct 15, 2003
- Messages: 1
- Status: Offline
J'ai besoin d'une fonction C + + qui prend une URL comme argument, et retourne une chaîne de 6 caractères de la première de la page Web de cette URL dirige à ...
(c'est-à -dire si je l'appliquer à http://www.ozzu.com , I devrait revenir à quelque chose comme les 6 premiers caractères de "OZZU WEBMASTER FORUM"...im en utilisant le Web est toutefois beaucoup moins esthétique programmé)
S'il vous plaît, aidez!
merci!
Manal
(c'est-à -dire si je l'appliquer à http://www.ozzu.com , I devrait revenir à quelque chose comme les 6 premiers caractères de "OZZU WEBMASTER FORUM"...im en utilisant le Web est toutefois beaucoup moins esthétique programmé)
S'il vous plaît, aidez!
merci!
Manal
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Octobre 16th, 2003, 11:20 pm
- b_heyer
- Web Master


- Inscription: Juin 15, 2003
- Messages: 4583
- Loc: Maryland
- Status: Offline
- dr nick
- Proficient


- Inscription: Sep 10, 2003
- Messages: 263
- Loc: Frankfurt
- Status: Offline
Code: [ Select ]
void first6(unsigned char *six, unsigned char *url) {
int i;
int offset;
/* with your offset, you can determine as of when you should copy 6 characters */
offset = 0; // if you know your url starts with "http://", you could set this value to 7
for (i = 0; i < 6; i++) {
six[i] = url[i+offset];
}
}
int i;
int offset;
/* with your offset, you can determine as of when you should copy 6 characters */
offset = 0; // if you know your url starts with "http://", you could set this value to 7
for (i = 0; i < 6; i++) {
six[i] = url[i+offset];
}
}
- void first6(unsigned char *six, unsigned char *url) {
- int i;
- int offset;
- /* with your offset, you can determine as of when you should copy 6 characters */
- offset = 0; // if you know your url starts with "http://", you could set this value to 7
- for (i = 0; i < 6; i++) {
- six[i] = url[i+offset];
- }
- }
OK code, ce n'est pas grand, mais tellement loin de ce que le youve a demandé il devrait fournir une base de départ. Youll devriez l'affiner pour tester si les URL commence par http ou quelque chose. Aussi, notez que je n'ai pas testé si l'URL est effectivement 6 caractères, de sorte que vous voudrez également de test pour ça.
- dr nick
- Proficient


- Inscription: Sep 10, 2003
- Messages: 263
- Loc: Frankfurt
- Status: Offline
- Bigwebmaster
- Site Admin


- Inscription: Déc 20, 2002
- Messages: 8922
- Loc: Seattle, WA & Phoenix, AZ
- Status: Offline
Puits qui pourrait prendre un peu de travail pour créer cette fonction, puisque c'est faire une chose il ya peu. D'abord, il doit demander à l'URL de page Web. Après cela, il doit analyser à travers la source de la page qu'il a demandé de trouver ce qui est dans la balise title. Enfin, il aurait besoin d'utiliser uniquement les 6 premiers caractères de ce qui est dans la balise title. De toute façon je ne vais pas écrire tout ça pour vous, mais je peux vous donner quelques éléments qui mai aider. Vous pouvez probablement utiliser une partie de ce que le Dr Nick vous avez donné à la présente. Voici comment vous pouvez demander une URL en C + + et récupérer la sortie de ce que vous demandez:
Vous aurez aussi besoin de ces classes / fichiers d'entête.
Voici clientsocket.h
Voici clientsocket.cpp
Code: [ Select ]
string serverReply;
//connect to server
try {
ClientSocket clientSocket("www.yourdomain.com", 80, 2);
try {
clientSocket << "GET http://www.yourdomain.com/ HTTP/1.0\n";
clientSocket << "Host: www.yourdomain.com\n\n";
clientSocket >> serverReply;
}
catch(SocketException& e) {
cout << "Exception was caught: " << e.description() << "<br>" << endl;
}
cout << "Response from server: " << serverReply << "<br><br>" << endl;
}
catch(SocketException& e) {
cout << "Exception was caught: " << e.description() << "<br>" << endl;
}
//connect to server
try {
ClientSocket clientSocket("www.yourdomain.com", 80, 2);
try {
clientSocket << "GET http://www.yourdomain.com/ HTTP/1.0\n";
clientSocket << "Host: www.yourdomain.com\n\n";
clientSocket >> serverReply;
}
catch(SocketException& e) {
cout << "Exception was caught: " << e.description() << "<br>" << endl;
}
cout << "Response from server: " << serverReply << "<br><br>" << endl;
}
catch(SocketException& e) {
cout << "Exception was caught: " << e.description() << "<br>" << endl;
}
- string serverReply;
- //connect to server
- try {
- ClientSocket clientSocket("www.yourdomain.com", 80, 2);
- try {
- clientSocket << "GET http://www.yourdomain.com/ HTTP/1.0\n";
- clientSocket << "Host: www.yourdomain.com\n\n";
- clientSocket >> serverReply;
- }
- catch(SocketException& e) {
- cout << "Exception was caught: " << e.description() << "<br>" << endl;
- }
- cout << "Response from server: " << serverReply << "<br><br>" << endl;
- }
- catch(SocketException& e) {
- cout << "Exception was caught: " << e.description() << "<br>" << endl;
- }
Vous aurez aussi besoin de ces classes / fichiers d'entête.
Voici clientsocket.h
Code: [ Select ]
#ifndef __clientsocket_h
#define __clientsocket_h // Prevent multiple #includes
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 50000;
class Socket {
private:
int m_sock;
sockaddr_in m_addr;
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind (const int port);
bool listen() const;
bool accept (Socket&) const;
// Client initialization
bool connect (const std::string host, const int port, const int timeOut);
// Data Transimission
bool send (const std::string) const;
int recv (std::string&) const;
void set_non_blocking (const bool);
bool is_valid() const { return m_sock != -1; }
};
class ClientSocket : private Socket {
public:
ClientSocket (std::string host, int port, int timeOut);
virtual ~ClientSocket(){};
const ClientSocket& operator << (const std::string&) const;
const ClientSocket& operator >> (std::string&) const;
};
class SocketException {
private:
std::string m_s;
public:
SocketException(std::string s) : m_s(s) {};
~SocketException(){};
std::string description() { return m_s; }
};
#endif // __clientsocket_h
#define __clientsocket_h // Prevent multiple #includes
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 50000;
class Socket {
private:
int m_sock;
sockaddr_in m_addr;
public:
Socket();
virtual ~Socket();
// Server initialization
bool create();
bool bind (const int port);
bool listen() const;
bool accept (Socket&) const;
// Client initialization
bool connect (const std::string host, const int port, const int timeOut);
// Data Transimission
bool send (const std::string) const;
int recv (std::string&) const;
void set_non_blocking (const bool);
bool is_valid() const { return m_sock != -1; }
};
class ClientSocket : private Socket {
public:
ClientSocket (std::string host, int port, int timeOut);
virtual ~ClientSocket(){};
const ClientSocket& operator << (const std::string&) const;
const ClientSocket& operator >> (std::string&) const;
};
class SocketException {
private:
std::string m_s;
public:
SocketException(std::string s) : m_s(s) {};
~SocketException(){};
std::string description() { return m_s; }
};
#endif // __clientsocket_h
- #ifndef __clientsocket_h
- #define __clientsocket_h // Prevent multiple #includes
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <string>
- #include <arpa/inet.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/time.h>
- const int MAXHOSTNAME = 200;
- const int MAXCONNECTIONS = 5;
- const int MAXRECV = 50000;
- class Socket {
- private:
- int m_sock;
- sockaddr_in m_addr;
- public:
- Socket();
- virtual ~Socket();
- // Server initialization
- bool create();
- bool bind (const int port);
- bool listen() const;
- bool accept (Socket&) const;
- // Client initialization
- bool connect (const std::string host, const int port, const int timeOut);
- // Data Transimission
- bool send (const std::string) const;
- int recv (std::string&) const;
- void set_non_blocking (const bool);
- bool is_valid() const { return m_sock != -1; }
- };
- class ClientSocket : private Socket {
- public:
- ClientSocket (std::string host, int port, int timeOut);
- virtual ~ClientSocket(){};
- const ClientSocket& operator << (const std::string&) const;
- const ClientSocket& operator >> (std::string&) const;
- };
- class SocketException {
- private:
- std::string m_s;
- public:
- SocketException(std::string s) : m_s(s) {};
- ~SocketException(){};
- std::string description() { return m_s; }
- };
- #endif // __clientsocket_h
Voici clientsocket.cpp
Code: [ Select ]
#include "clientsocket.h"
Socket::Socket() : m_sock (-1)
{
memset (&m_addr, 0, sizeof(m_addr));
}
Socket::~Socket()
{
if (is_valid())
::close (m_sock);
}
bool Socket::create()
{
m_sock = socket(AF_INET, SOCK_STREAM, 0);
if (! is_valid())
return false;
// TIME_WAIT - argh
int on = 1;
if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
return false;
return true;
}
bool Socket::bind (const int port)
{
if (! is_valid())
return false;
m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons(port);
int bind_return = ::bind(m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr));
if(bind_return == -1)
return false;
return true;
}
bool Socket::listen() const
{
if(! is_valid())
return false;
int listen_return = ::listen(m_sock, MAXCONNECTIONS);
if (listen_return == -1)
return false;
return true;
}
bool Socket::accept(Socket& new_socket) const
{
int addr_length = sizeof(m_addr);
new_socket.m_sock = ::accept (m_sock, (sockaddr *) &m_addr, (socklen_t *) &addr_length);
if (new_socket.m_sock <= 0)
return false;
else
return true;
}
bool Socket::send(const std::string s) const
{
int status = ::send(m_sock, s.c_str(), s.size(), 0);
if (status == -1)
return false;
else
return true;
}
int Socket::recv(std::string& s) const
{
char buf[MAXRECV + 1];
s = "";
memset(buf, 0, MAXRECV + 1);
int status = ::recv(m_sock, buf, MAXRECV, 0);
if (status == -1) {
// std::cout << "status == -1 errno == " << errno << ", " << strerror(errno) << " in Socket::recv\n";
return 0;
}
else if (status == 0)
return 0;
else {
s = buf;
return status;
}
}
bool Socket::connect(const std::string host, const int port, const int timeOut)
{
struct hostent *hp;
struct timeval tv;
fd_set writefds;
tv.tv_sec = timeOut;
tv.tv_usec = 500000;
FD_ZERO(&writefds);
FD_SET(m_sock, &writefds);
if (! is_valid()) return false;
if((hp = gethostbyname(host.c_str()))) {
memset((char *) &m_addr, 0, sizeof(m_addr));
memmove((char *) &m_addr.sin_addr, hp->h_addr, hp->h_length);
}
else return false;
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(port);
if (errno == EAFNOSUPPORT) return false;
::connect(m_sock, (sockaddr *) &m_addr, sizeof (m_addr));
select(m_sock+1, NULL, &writefds, NULL, &tv);
if (FD_ISSET(m_sock, &writefds))
return true;
else
return false;
}
void Socket::set_non_blocking(const bool b)
{
int opts;
opts = fcntl (m_sock, F_GETFL);
if (opts < 0)
return;
if (b)
opts = (opts | O_NONBLOCK);
else
opts = (opts & ~O_NONBLOCK);
fcntl (m_sock, F_SETFL,opts);
}
ClientSocket::ClientSocket(std::string host, int port, int timeOut)
{
if(! Socket::create())
throw SocketException ( "Could not create client socket." );
Socket::set_non_blocking(true);
if(! Socket::connect (host, port, timeOut))
throw SocketException ("Could not bind to port.");
Socket::set_non_blocking(false);
}
const ClientSocket& ClientSocket::operator << (const std::string& s) const
{
if (! Socket::send (s))
throw SocketException ("Could not write to socket.");
return *this;
}
const ClientSocket& ClientSocket::operator >> (std::string& s) const
{
if (! Socket::recv(s))
throw SocketException("Could not read from socket.");
return *this;
}
Socket::Socket() : m_sock (-1)
{
memset (&m_addr, 0, sizeof(m_addr));
}
Socket::~Socket()
{
if (is_valid())
::close (m_sock);
}
bool Socket::create()
{
m_sock = socket(AF_INET, SOCK_STREAM, 0);
if (! is_valid())
return false;
// TIME_WAIT - argh
int on = 1;
if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
return false;
return true;
}
bool Socket::bind (const int port)
{
if (! is_valid())
return false;
m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons(port);
int bind_return = ::bind(m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr));
if(bind_return == -1)
return false;
return true;
}
bool Socket::listen() const
{
if(! is_valid())
return false;
int listen_return = ::listen(m_sock, MAXCONNECTIONS);
if (listen_return == -1)
return false;
return true;
}
bool Socket::accept(Socket& new_socket) const
{
int addr_length = sizeof(m_addr);
new_socket.m_sock = ::accept (m_sock, (sockaddr *) &m_addr, (socklen_t *) &addr_length);
if (new_socket.m_sock <= 0)
return false;
else
return true;
}
bool Socket::send(const std::string s) const
{
int status = ::send(m_sock, s.c_str(), s.size(), 0);
if (status == -1)
return false;
else
return true;
}
int Socket::recv(std::string& s) const
{
char buf[MAXRECV + 1];
s = "";
memset(buf, 0, MAXRECV + 1);
int status = ::recv(m_sock, buf, MAXRECV, 0);
if (status == -1) {
// std::cout << "status == -1 errno == " << errno << ", " << strerror(errno) << " in Socket::recv\n";
return 0;
}
else if (status == 0)
return 0;
else {
s = buf;
return status;
}
}
bool Socket::connect(const std::string host, const int port, const int timeOut)
{
struct hostent *hp;
struct timeval tv;
fd_set writefds;
tv.tv_sec = timeOut;
tv.tv_usec = 500000;
FD_ZERO(&writefds);
FD_SET(m_sock, &writefds);
if (! is_valid()) return false;
if((hp = gethostbyname(host.c_str()))) {
memset((char *) &m_addr, 0, sizeof(m_addr));
memmove((char *) &m_addr.sin_addr, hp->h_addr, hp->h_length);
}
else return false;
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(port);
if (errno == EAFNOSUPPORT) return false;
::connect(m_sock, (sockaddr *) &m_addr, sizeof (m_addr));
select(m_sock+1, NULL, &writefds, NULL, &tv);
if (FD_ISSET(m_sock, &writefds))
return true;
else
return false;
}
void Socket::set_non_blocking(const bool b)
{
int opts;
opts = fcntl (m_sock, F_GETFL);
if (opts < 0)
return;
if (b)
opts = (opts | O_NONBLOCK);
else
opts = (opts & ~O_NONBLOCK);
fcntl (m_sock, F_SETFL,opts);
}
ClientSocket::ClientSocket(std::string host, int port, int timeOut)
{
if(! Socket::create())
throw SocketException ( "Could not create client socket." );
Socket::set_non_blocking(true);
if(! Socket::connect (host, port, timeOut))
throw SocketException ("Could not bind to port.");
Socket::set_non_blocking(false);
}
const ClientSocket& ClientSocket::operator << (const std::string& s) const
{
if (! Socket::send (s))
throw SocketException ("Could not write to socket.");
return *this;
}
const ClientSocket& ClientSocket::operator >> (std::string& s) const
{
if (! Socket::recv(s))
throw SocketException("Could not read from socket.");
return *this;
}
- #include "clientsocket.h"
- Socket::Socket() : m_sock (-1)
- {
- memset (&m_addr, 0, sizeof(m_addr));
- }
- Socket::~Socket()
- {
- if (is_valid())
- ::close (m_sock);
- }
- bool Socket::create()
- {
- m_sock = socket(AF_INET, SOCK_STREAM, 0);
- if (! is_valid())
- return false;
- // TIME_WAIT - argh
- int on = 1;
- if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1)
- return false;
- return true;
- }
- bool Socket::bind (const int port)
- {
- if (! is_valid())
- return false;
- m_addr.sin_family = AF_INET;
- m_addr.sin_addr.s_addr = INADDR_ANY;
- m_addr.sin_port = htons(port);
- int bind_return = ::bind(m_sock, (struct sockaddr *) &m_addr, sizeof(m_addr));
- if(bind_return == -1)
- return false;
- return true;
- }
- bool Socket::listen() const
- {
- if(! is_valid())
- return false;
- int listen_return = ::listen(m_sock, MAXCONNECTIONS);
- if (listen_return == -1)
- return false;
- return true;
- }
- bool Socket::accept(Socket& new_socket) const
- {
- int addr_length = sizeof(m_addr);
- new_socket.m_sock = ::accept (m_sock, (sockaddr *) &m_addr, (socklen_t *) &addr_length);
- if (new_socket.m_sock <= 0)
- return false;
- else
- return true;
- }
- bool Socket::send(const std::string s) const
- {
- int status = ::send(m_sock, s.c_str(), s.size(), 0);
- if (status == -1)
- return false;
- else
- return true;
- }
- int Socket::recv(std::string& s) const
- {
- char buf[MAXRECV + 1];
- s = "";
- memset(buf, 0, MAXRECV + 1);
- int status = ::recv(m_sock, buf, MAXRECV, 0);
- if (status == -1) {
- // std::cout << "status == -1 errno == " << errno << ", " << strerror(errno) << " in Socket::recv\n";
- return 0;
- }
- else if (status == 0)
- return 0;
- else {
- s = buf;
- return status;
- }
- }
- bool Socket::connect(const std::string host, const int port, const int timeOut)
- {
- struct hostent *hp;
- struct timeval tv;
- fd_set writefds;
- tv.tv_sec = timeOut;
- tv.tv_usec = 500000;
- FD_ZERO(&writefds);
- FD_SET(m_sock, &writefds);
- if (! is_valid()) return false;
- if((hp = gethostbyname(host.c_str()))) {
- memset((char *) &m_addr, 0, sizeof(m_addr));
- memmove((char *) &m_addr.sin_addr, hp->h_addr, hp->h_length);
- }
- else return false;
- m_addr.sin_family = AF_INET;
- m_addr.sin_port = htons(port);
- if (errno == EAFNOSUPPORT) return false;
- ::connect(m_sock, (sockaddr *) &m_addr, sizeof (m_addr));
- select(m_sock+1, NULL, &writefds, NULL, &tv);
- if (FD_ISSET(m_sock, &writefds))
- return true;
- else
- return false;
- }
- void Socket::set_non_blocking(const bool b)
- {
- int opts;
- opts = fcntl (m_sock, F_GETFL);
- if (opts < 0)
- return;
- if (b)
- opts = (opts | O_NONBLOCK);
- else
- opts = (opts & ~O_NONBLOCK);
- fcntl (m_sock, F_SETFL,opts);
- }
- ClientSocket::ClientSocket(std::string host, int port, int timeOut)
- {
- if(! Socket::create())
- throw SocketException ( "Could not create client socket." );
- Socket::set_non_blocking(true);
- if(! Socket::connect (host, port, timeOut))
- throw SocketException ("Could not bind to port.");
- Socket::set_non_blocking(false);
- }
- const ClientSocket& ClientSocket::operator << (const std::string& s) const
- {
- if (! Socket::send (s))
- throw SocketException ("Could not write to socket.");
- return *this;
- }
- const ClientSocket& ClientSocket::operator >> (std::string& s) const
- {
- if (! Socket::recv(s))
- throw SocketException("Could not read from socket.");
- return *this;
- }
Ozzu Hosting - Want your website on a fast server like Ozzu?
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 5 messages
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 122 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
