XMPP 注册正在生成 400 错误请求错误
本文关键字:错误 请求 注册 XMPP | 更新日期: 2023-09-27 18:36:29
我正在尝试使用此库在Windows Phone 8.1中实现XMPP Chat客户端。由于该库不支持新用户注册,因此我正在尝试将其包含在源代码中。我使用单独的ServerFeaturesState
进行注册并将其命名为 RegisterServerFeaturesState
。现在,当我发送注册节时,我收到错误代码 400。
这是我发送的内容 -
<iq id="U4" type="set" xmlns="jabber:client">
<query xmlns="jabber:iq:register">
<username>user@domain.lt/RESOURCE</username>
<password>password1234</password>
</query>
</iq>
这就是我得到的回报——
<iq from="domain.lt" id="U5" type="error" xmlns="jabber:client">
<query xmlns="jabber:iq:register">
<username>user@domain.lt/RESOURCE</username>
<password>password1234</password>
</query>
<error code="400" type="modify">
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
</error>
<!--[TS]1/6/2015 4:45:37 PM-->
</iq>
我在我的状态的执行方法中使用以下代码 -
public override void Execute(Tag data = null)
{
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "Starting RegisterServerFeaturesState Execution "+data);
#endif
tags.streams.features features = null;
tags.jabber.client.iq iqRoot = null;
if (data is tags.streams.stream)
{
tags.streams.stream stream = data as tags.streams.stream;
if (!stream.version.StartsWith("1."))
{
Manager.Events.Error(this, ErrorType.XMPPVersionNotSupported, ErrorPolicyType.Deactivate, "Expecting stream:features from 1.x server");
return;
}
features = stream.features;
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "RegisterServerFeaturesState stream");
#endif
}
else if (data is tags.streams.error)
{
var error = data as tags.streams.error;
string message = string.Empty;
if( error.HasElements)
{
var text = error.Element<tags.xmpp_streams.text>(tags.xmpp_streams.Namespace.text);
if (text != null)
{
message = text.Value;
}
else if( error.Elements().Count() > 0 )
{
var element = error.Elements().First();
if( element != null )
message = element.Name.LocalName;
}
}
Manager.Events.Error(this, ErrorType.ServerError, ErrorPolicyType.Reconnect, message);
return;
}
else if (data is tags.streams.features)
{
features = data as tags.streams.features;
}
else if (data is tags.jabber.client.iq)
{
iqRoot = data as tags.jabber.client.iq;
}
if (features != null)
{
if (features.starttls != null && Manager.Settings.SSL)
{
Manager.State = new StartTLSState(Manager);
tags.xmpp_tls.starttls tls = new tags.xmpp_tls.starttls();
Manager.Connection.Send(tls);
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "RegisterServerFeaturesState starttls");
#endif
return;
}
if (!Manager.IsRegistrationFeatureRequested)
{
var iq = new tags.jabber.client.iq();
iq.type = tags.jabber.client.iq.typeEnum.get;
var query = new tags.jabber.iq.register.query();
iq.Add(query);
Manager.Connection.Send(iq);
Manager.IsRegistrationFeatureRequested = true;
registrationRequestID = iq.id;
return;
}
}
else if (iqRoot != null)
{
if (Manager.IsRegistrationFeatureRequested)
{
if (iqRoot.type == tags.jabber.client.iq.typeEnum.result)
{
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "Registration Instructions: Step 1");
#endif
var query = iqRoot.Element<tags.jabber.iq.register.query>(tags.jabber.iq.register.Namespace.query);
if (query != null)
{
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "Registration Instructions: Step 2");
#endif
var instruction = query.Element<tags.jabber.iq.register.instructions>(tags.jabber.iq.register.Namespace.instructions);
if (instruction != null)
{
#if DEBUG
Manager.Events.LogMessage(this, LogType.Debug, "Registration Instructions: " + instruction.Value);
#endif
var username = query.Element<tags.jabber.iq.register.username>(tags.jabber.iq.register.Namespace.username);
var password = query.Element<tags.jabber.iq.register.password>(tags.jabber.iq.register.Namespace.password);
if (username != null && password != null)
{
var iq = new tags.jabber.client.iq();
iq.type = tags.jabber.client.iq.typeEnum.set;
iq.to = iqRoot.from;
var requestQuery = new tags.jabber.iq.register.query();
var userTag = new tags.jabber.iq.register.username();
userTag.Value = Manager.Settings.Id;
var passTag = new tags.jabber.iq.register.password();
passTag.Value = Manager.Settings.Password;
requestQuery.Add(userTag);
requestQuery.Add(passTag);
iq.Add(requestQuery);
Manager.Connection.Send(iq);
Manager.IsRegistrationRequestSent = true;
return;
}
}
}
}
}
}
}
}
}
以下是获取注册说明的内容
<iq from="domain.lt" id="U3" type="result" xmlns="jabber:client">
<query xmlns="jabber:iq:register">
<instructions>Choose a username and password to register with this server</instructions>
<username></username>
<password />
</query>
我不确定错误代码 400 是什么意思。我是否忘记了注册节中的某些内容?
在注册请求中,指定完整的 JID(用户名、服务器和资源):
<username>user@domain.lt/RESOURCE</username>
但是,服务器通常只期望那里有一个用户名,所以请尝试以下操作:
<username>user</username>
我看不出这在 XEP-0077 中明确说明,但至少这是示例所显示的。