XMPPFramework- Error “405” in File Transfer

2019-09-14 02:52发布

问题:

I am trying to implement file transfer using XMPPFramework in iOS. I am sending stanzas to user who is in my roster as following.


-(void)sendFile
{
    NSString *filePath = fileNameToBeUploaded;
    CFStringRef fileExtension = (__bridge CFStringRef)[filePath pathExtension];
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
    CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
    CFRelease(UTI);
    NSString *MIMETypeString = (__bridge_transfer NSString *)MIMEType;
    NSString *URL = fileNameToBeUploaded;

    NSError *AttributesError = nil;
    NSDictionary *FileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&AttributesError];
    NSNumber *FileSizeNumber = [FileAttributes objectForKey:NSFileSize];
    NSArray *splitter = [MIMETypeString componentsSeparatedByString:@"/"];
    NSString *extension = [splitter objectAtIndex:1];
    NSString *mediaType = @"image";
    long FileSize = [FileSizeNumber longValue];

    // XMPPUserCoreDataStorageObject *user = [[self appDelegate].xmppRosterStorage userForJID:[XMPPJID jidWithString:self.chatWithUser] xmppStream:[self appDelegate].xmppStream managedObjectContext:[self appDelegate].managedObjectContex_roster];
    // Create message


    self.streamId = [self generateIDWithPrefix:@"ip_"];
    self.requestId = [self generateIDWithPrefix:@"jsi_"];

    NSXMLElement *si = [NSXMLElement elementWithName:@"si" xmlns:@"http://jabber.org/protocol/si"];
    [si addAttributeWithName:@"id" stringValue:self.streamId];
    [si addAttributeWithName:@"mime-type" stringValue:MIMETypeString];
    [si addAttributeWithName:@"profile" stringValue:@"http://jabber.org/protocol/si/profile/file-transfer"];

    NSXMLElement *fileElement = [NSXMLElement elementWithName:@"file" xmlns:@"http://jabber.org/protocol/si/profile/file-transfer"];
    [fileElement addAttributeWithName:@"name" stringValue:[fileNameToBeUploaded lastPathComponent]];
    [fileElement addAttributeWithName:@"size" stringValue:[NSString stringWithFormat:@"%ld",FileSize]];
    [fileElement addAttributeWithName:@"desc" stringValue:@""];
    [si addChild:fileElement];

    NSXMLElement *feature = [NSXMLElement elementWithName:@"feature" xmlns:@"http://jabber.org/protocol/feature-neg"];

    NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
    [x addAttributeWithName:@"type" stringValue:@"from"];

    NSXMLElement *field = [NSXMLElement elementWithName:@"field"];
    [field addAttributeWithName:@"type" stringValue:@"list-single"];
    [field addAttributeWithName:@"var" stringValue:@"stream-method"];

    NSXMLElement *option = [NSXMLElement elementWithName:@"option"];
    NSXMLElement *bs = [NSXMLElement elementWithName:@"value" xmlns:@"http://jabber.org/protocol/bytestreams"];

    [option addChild:bs];
    [field addChild:option];
    [x addChild:field];
    [feature addChild:x];
    [si addChild:feature];

    XMPPJID *toJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@/iPhone",self.chatWithUser]];
    XMPPIQ *iqtoSend = [XMPPIQ iqWithType:@"set" to:toJid elementID:self.requestId child:si];

    if (self.fileInfo)
    {
        self.fileInfo = nil;
    }

    self.fileInfo = [[FileInfo alloc] initWithFileName:fileNameToBeUploaded mediaType:mediaType mimeType:MIMETypeString size:FileSize localName:fileNameToBeUploaded IQ:[NSString stringWithFormat:@"%@",iqtoSend] fileNameAsSent:fileNameToBeUploaded sender:@""];
    [self appDelegate].isSending = YES;
    [[self appDelegate].xmppStream sendElement:iqtoSend];

}

Console shows the following

Sending stanzas

SEND: <iq type="set" to="tushar@54.186.107.171/iPhone" id="jsi_9579">
        <si xmlns="http://jabber.org/protocol/si" id="ip_4387" mime-type="image/png" profile="http://jabber.org/protocol/si/profile/file-transfer">
            <file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="11392014-143906437.png" size="166688" desc=""/>
                <feature xmlns="http://jabber.org/protocol/feature-neg">
                    <x xmlns="jabber:x:data" type="from">
                        <field type="list-single" var="stream-method">
                            <option>
                                <value xmlns="http://jabber.org/protocol/bytestreams"/>
                            </option>
                        </field>
                    </x>
                </feature>
            </si>
        </iq>

Response

RECV: <iq xmlns="jabber:client" type="error" id="jsi_9579" to="dev1@54.186.107.171/55879756" from="tushar@54.186.107.171/iPhone">
    <si xmlns="http://jabber.org/protocol/si" id="ip_4387" mime-type="image/png" profile="http://jabber.org/protocol/si/profile/file-transfer">
        <file xmlns="http://jabber.org/protovcol/si/profile/file-transfer" name="11392014-143906437.png" size="166688" desc=""/>
            <feature xmlns="http://jabber.org/protocol/feature-neg">
                <x xmlns="jabber:x:data" type="from">
                    <field type="list-single" var="stream-method">
                        <option>
                            <value xmlns="http://jabber.org/protocol/bytestreams"/>
                        </option>
                    </field>
                </x>
            </feature>
        </si>
        <error code="405" type="cancel"><not-allowed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
        </error><
    </iq>

My JID is dev1@54.186.107.171. JI of user whom I want to send file is tushar@54.186.107.171.

Why I am getting error 405? Is any process is required to do file transfer? Have I missed something?

回答1:

Correct following line

[x addAttributeWithName:@"type" stringValue:@"from"];

With

[x addAttributeWithName:@"type" stringValue:@"form"];

Here you have written "from" instead of "form". For more details you can visit following link: http://xmpp.org/extensions/xep-0096.html