-6ren"> -我正在尝试在 iOS 中使用 XMPPFramework 实现文件传输。我正在向我名册中的用户发送节,如下所示。 -(void)sendFile { NSString *filePath = -6ren">
gpt4 book ai didi

ios - XMPPFramework- 文件传输中的错误 "405"

转载 作者:行者123 更新时间:2023-11-29 02:49:53 24 4
gpt4 key购买 nike

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


-(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?文件传输需要任何过程吗?我错过了什么吗?

最佳答案

正确的下一行

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

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

这里你写的是“from”而不是“form”。更多详情,您可以访问以下链接:http://xmpp.org/extensions/xep-0096.html

关于ios - XMPPFramework- 文件传输中的错误 "405"<not-allowed>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24695222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com