简体   繁体   中英

XMPP Registration is generating 400 bad request error

I am trying to implement a XMPP Chat client in Windows Phone 8.1 using this library . Since this library does not support new user registration, I am trying to include this in the source code. I've used separate ServerFeaturesState for registration and named it as RegisterServerFeaturesState . Now when I send registration stanza, I get error code 400.
Here's what I am sending -

<iq id="U4" type="set" xmlns="jabber:client">
  <query xmlns="jabber:iq:register">
    <username>user@domain.lt/RESOURCE</username>
    <password>password1234</password>
  </query>
</iq>

and here's what I am getting in return -

<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>

I am using the following code in Execute method of my state -

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;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Here's what getting registration instruction tells

<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>

I am not sure what Error code 400 means. Am I forgetting something in the registration stanza?

In your registration request, you specify a full JID (username, server and resource):

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

However, servers usually expect just a username there, so try this instead:

<username>user</username>

I can't see that this is explicitly spelled out in XEP-0077 , but at least it's what the examples are showing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM