XMPPFramework-错误&#34; 405&#34;文件传输中的<not-allowed> </not-allowed>

时间:2014-07-11 10:06:00

标签: ios xmpp file-transfer xmppframework

我正在尝试使用iOS中的XMPPFramework实现文件传输。我正在向我的名单中的用户发送stanzas,如下所示。


-(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];

}

控制台显示以下内容

发送诗节

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>

响应

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>

我的JID是dev1@54.186.107.171。 我想发送文件的用户的JI是tushar@54.186.107.171。

为什么我收到错误405? 是否需要进行文件传输?我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

纠正以下行

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

使用

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

这里你写的是“from”而不是“form”。有关详细信息,请访问以下链接:http://xmpp.org/extensions/xep-0096.html