我的服务器中有可能的字段:
<iq xmlns="jabber:client" from="vjud.company.com" to="testuser@company.com/iPhone" id="search1" type="result"><query xmlns="jabber:iq:search">
<instructions>You need an x:data capable client to search</instructions>
<x xmlns="jabber:x:data" type="form">
<title>Search users in vjud.company.com</title>
<instructions>Fill in the form to search for any matching Jabber User (Add * to the end of field to match substring)
</instructions>
<field type="text-single" label="User" var="user"/>
<field type="text-single" label="Full Name" var="fn"/>
<field type="text-single" label="Name" var="first"/>
<field type="text-single" label="Middle Name" var="middle"/>
<field type="text-single" label="Family Name" var="last"/>
<field type="text-single" label="Nickname" var="nick"/>
<field type="text-single" label="Birthday" var="bday"/>
<field type="text-single" label="Country" var="ctry"/>
<field type="text-single" label="City" var="locality"/>
<field type="text-single" label="Email" var="email"/>
<field type="text-single" label="Organization Name" var="orgname"/>
<field type="text-single" label="Organization Unit" var="orgunit"/>
</x>
</query>
</iq>
假设我想搜索JID为admin@company.com
组合请求将如下所示:
XMPPIQ *iq2 = [[XMPPIQ alloc] init];
[iq2 addAttributeWithName:@"type" stringValue:@"set"];
[iq2 addAttributeWithName:@"from" stringValue:@"testuser@company.com"];
[iq2 addAttributeWithName:@"to" stringValue:@"vjud.company.com"];
[iq2 addAttributeWithName:@"id" stringValue:@"search1"];
XMPPElement *query2 = [XMPPElement elementWithName:@"query"];
[query2 setXmlns:@"jabber:iq:search"];
XMPPElement *user = [XMPPElement elementWithName:@"user"];
[user setStringValue:@"admin"];
[iq2 addChild:query2];
[query addChild:user];
错误节:
<iq xmlns="jabber:client" from="vjud.company.com" to="testuser@company.com/iPhone" type="error" id="search1">
<query xmlns="jabber:iq:search"/>
<error code="400" type="modify">
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</iq>
所以,基本上有2个问题:
</query>
元素?- 服务器的响应应该如下所示 -
非常感谢@legoscia用户,如果请求组合正确,您将得到类似的结果(通知<item>
元素):
<iq xmlns="jabber:client" from="vjud.company.com" to="testuser@company.com/iPhone" id="search1" type="result»>
<query xmlns="jabber:iq:search"><x xmlns="jabber:x:data" type="result»>
<title>Search Results for vjud.company.com</title>
<reported>
<field type="text-single" label="Jabber ID" var="jid»/>
<field type="text-single" label="Full Name" var="fn»/>
<field type="text-single" label="Name" var="first»/>
<field type="text-single" label="Middle Name" var="middle»/>
<field type="text-single" label="Family Name" var="last"/><field type="text-single" label="Nickname" var="nick»/>
<field type="text-single" label="Birthday" var="bday»/>
<field type="text-single" label="Country" var="ctry»/>
<field type="text-single" label="City" var="locality»/>
<field type="text-single" label="Email" var="email»/>
<field type="text-single" label="Organization Name" var="orgname"/><field type="text-single" label="Organization Unit" var="orgunit»/>
</reported>
<item>
<field var="jid»>
<value>admin@company.com</value>
</field>
<field var="fn"><value/></field>
<field var="last"><value/></field>
<field var="first"><value/></field>
<field var="middle"><value/></field>
<field var="nick"><value/></field>
<field var="bday"><value/></field>
<field var="ctry"><value/></field>
<field var="locality"><value/></field>
<field var="email"><value/></field>
<field var="orgname"><value/></field>
<field var="orgunit"><value/></field></item>
</x>
</query>
</iq>
如果没有匹配项,您将只收到包含大量字段的<reported>
元素。您可能也希望查看this。
答案 0 :(得分:1)
此搜索服务不支持&#34; plain&#34; XEP-0055搜索字段,但要求您提交响应中返回的x:data
表单&#34; get&#34;请求;见XEP-0004。
您可以通过结果不包含任何建议的搜索字段作为查询元素的子元素来实现这一点(参见XEP-0055的示例2),并且<instructions/>
元素也是如此。这意味着您需要查看<x xmlns="jabber:x:data" type="form">
元素并提交它。
因此,您需要发送类似的内容:
<iq type='set'
from='juliet@capulet.com/balcony'
to='characters.shakespeare.lit'
id='search4'
xml:lang='en'>
<query xmlns='jabber:iq:search'>
<x xmlns='jabber:x:data' type='submit'>
<field var='user'>
<value>admin</value>
</field>
</x>
</query>
</iq>
(这是来自XEP-0055的示例8,已修改为搜索用户名为&#34; admin&#34 ;.)