XMPP注册生成400错误请求错误

时间:2015-01-06 11:32:33

标签: c# xmpp bad-request

我正在尝试使用this library在Windows Phone 8.1中实现XMPP聊天客户端。由于此库不支持新用户注册,因此我尝试将其包含在源代码中。我已使用单独的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的含义。我在注册节中遗忘了什么吗?

1 个答案:

答案 0 :(得分:1)

在您的注册请求中,您指定一个完整的JID(用户名,服务器和资源):

<username>user@domain.lt/RESOURCE</username>

但是,服务器通常只需要一个用户名,所以请尝试这样做:

<username>user</username>

我看不出这是XEP-0077中明确说明的,但至少它是示例所显示的内容。