我的任务是创建一个必须与服务器上的其他客户端通信的C ++ XMPP客户端。我使用Gloox库编写了一个简单的C ++ XMPP客户端,在服务器上注册一个新用户。 我使用的服务器是Openfire,IDE是VC ++ 2008.
// register_example.cpp
class RegTest : public RegistrationHandler, ConnectionListener, LogHandler
{
public:
RegTest() {}
virtual ~RegTest() {}
void start()
{
j = new Client( "localhost" );
j->disableRoster();
j->registerConnectionListener( this );
m_reg = new Registration( j );
m_reg->registerRegistrationHandler( this );
j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
j->connect();
delete( m_reg );
delete( j );
}
virtual void onConnect()
{
m_reg->fetchRegistrationFields();
}
virtual void onDisconnect( ConnectionError e ){
printf( "register_test: disconnected: %d\n", e ); }
virtual bool onTLSConnect( const CertInfo& info )
{
printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n",
info.status, info.issuer.c_str(), info.server.c_str(),
info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
info.compression.c_str() );
return true;
}
virtual void handleRegistrationFields( const JID& /*from*/, int fields, std::string instructions )
{
printf( "fields: %d\ninstructions: %s\n", fields, instructions.c_str() );
RegistrationFields vals;
vals.username = "testuser";
vals.password = "testpwd";
m_reg->createAccount( fields, vals );
}
virtual void handleRegistrationResult( const JID& /*from*/, RegistrationResult result )
{
printf( "result: %d\n", result );
j->disconnect();
}
virtual void handleAlreadyRegistered( const JID& /*from*/ )
{
printf( "the account already exists.\n" );
}
virtual void handleDataForm( const JID& /*from*/, const DataForm& /*form*/ )
{
printf( "datForm received\n" );
}
virtual void handleOOB( const JID& /*from*/, const OOB& oob )
{
printf( "OOB registration requested. %s: %s\n", oob.desc().c_str(), oob.url().c_str() );
}
virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
{
printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
}
private:
Registration *m_reg;
Client *j;
};
int _tmain(int argc, _TCHAR* argv[])
{
RegTest *r = new RegTest();
r->start();
return 0;
}
代码构建成功,但是当我运行代码时,我得到以下错误代码400 ---错误请求。请告诉我出了什么问题? 日志 -
**log: level: 0, area: 8, This is gloox 1.0, connecting to localhost:-1...
log: level: 1, area: 32, DnsQuery_UTF8() failed: 9003
log: level: 1, area: 32, Notice: no SRV record found for localhost, using defaul
t port.
log: level: 0, area: 32, Connecting to localhost (127.0.0.1:5222)
log: level: 0, area: 32, Connected to localhost (127.0.0.1:5222)
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xml
ns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanis
m>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanis
m></mechanisms><compression xmlns='http://jabber.org/features/compress'><method>
zlib</method></compression><auth xmlns='http://jabber.org/features/iq-auth'/><re
gister xmlns='http://jabber.org/features/iq-register'/></stream:features>
log: level: 0, area: 262144, <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
log: level: 0, area: 131072, <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
log: level: 0, area: 4, starting TLS handshake...
status: -842150451
issuer: CN=ds-001d92dde38d
peer: CN=ds-001d92dde38d
protocol: TLSv1
mac: SHA
cipher: AES_128
compression:
log: level: 0, area: 8, connection encryption active
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DI
GEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism>
<mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns='http://jabber.or
g/features/compress'><method>zlib</method></compression><auth xmlns='http://jabb
er.org/features/iq-auth'/><register xmlns='http://jabber.org/features/iq-registe
r'/></stream:features>
log: level: 0, area: 262144, <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mech
anism='ANONYMOUS'/>
log: level: 0, area: 131072, <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
log: level: 0, area: 4, SASL authentication successful
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><compression xmlns='http://jabber.org/features/compress'><method>z
lib</method></compression><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><sessi
on xmlns='urn:ietf:params:xml:ns:xmpp-session'/></stream:features>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00000029' type='set' from='loc
alhost' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><re
source/></bind></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:00000029' to='ds
-001d92dde38d/ab49a54c'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>ab49
a54c@ds-001d92dde38d/ab49a54c</jid></bind></iq>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00004823' type='set' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><session xmlns='urn:ietf:p
arams:xml:ns:xmpp-session'/></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:00004823' to='ab
49a54c@ds-001d92dde38d/ab49a54c'/>
log: level: 0, area: 262144, <presence from='ab49a54c@ds-001d92dde38d/ab49a54c'
xmlns='jabber:client'><priority>0</priority><c xmlns='http://jabber.org/protocol
/caps' hash='sha-1' node='http://camaya.net/gloox' ver='1LpOuB49QCQE6BcZoAF5tBoA
Ldg='/></presence>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:000018be' type='get' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><query xmlns='jabber:iq:re
gister'/></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:000018be' to='ab
49a54c@ds-001d92dde38d/ab49a54c'><query xmlns='jabber:iq:register'><username/><p
assword/><email/><name/><x xmlns='jabber:x:data' type='form'><title>XMPP Client
Registration</title><instructions>Please provide the following information</inst
ructions><field var='FORM_TYPE' type='hidden'><value>jabber:iq:register</value><
/field><field var='username' type='text-single' label='Username'><required/></fi
eld><field var='name' type='text-single' label='Full name'/><field var='email' t
ype='text-single' label='Email'/><field var='password' type='text-private' label
='Password'><required/></field></x></query></iq>
datForm received
fields: 77
instructions:
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00006784' type='set' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><query xmlns='jabber:iq:re
gister'><username>testuser</username><password>testpwd</password><name/><email/>
</query></iq>
log: level: 0, area: 131072, <iq type='error' id='uid:501a3ee9:00006784' to='ab4
9a54c@ds-001d92dde38d/ab49a54c'><query xmlns='jabber:iq:register'><username>test
user</username><password>testpwd</password><name/><email/></query><error code='4
00' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></er
ror></iq>
result: 4
log: level: 0, area: 262144, </stream:stream>
register_test: disconnected: 17**
答案 0 :(得分:0)
服务器需要的信息比“jabber:iq:register”命名空间的架构支持的信息多,因此服务器向您发送了数据表单。您必须返回填充的数据表单,该表单优先于“jabber:iq:register”的字段。它在XEP-0077(带内注册)的Extensibility部分中定义。