3

我正在开发一个 iOS iPad 应用程序来连接到 Sharepoint。我正在使用 ASIHTTPRequest 框架执行此操作。我的代码如下所示:

NSURL *url = [NSURL URLWithString:@"https://SHAREPOINTURL"];

NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
"<soap12:Body>\n"
"<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />\n"
"</soap12:Body>\n"
"</soap12:Envelope>\n";

asiRequest = [ASIHTTPRequest requestWithURL:url];

[asiRequest setUsername:@"USERNAME"];
[asiRequest setPassword:@"PASSWORD"];

[asiRequest setDelegate:self];
[asiRequest appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[asiRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[asiRequest startAsynchronous];

到目前为止,一切都很好,但是当我调试它时,我得到一个 403 FORBIDDEN 错误。为什么?在我看来,我不认为错误是用户名和密码。也许请求有问题?

4

2 回答 2

0

查看您的 SOAP 信封和错误消息,我认为您缺少SOAPAction请求中的标头。

下面的代码显示了如何创建请求以获取 Sharepoint 实例中的所有列表的示例。我正在使用 AFNetworking,某些部分可能不适用于 ASIHTTPRequest 框架。

...

NSMutableURLRequest *request = [self requestWithMethod:@"POST"
                                                  path:@"_vti_bin/Lists.asmx"
                                            parameters:nil];

NSString *soapEnvelope = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  @"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
  @"<soap:Body>"
  @"<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />"
  @"</soap:Body>"
  @"</soap:Envelope>";

// Set headers
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"http://schemas.microsoft.com/sharepoint/soap/GetListCollection" forHTTPHeaderField:@"SOAPAction"];

// Content length
NSString *contentLength = [NSString stringWithFormat:@"%d", soapEnvelope.length];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];

// Set body
[request setHTTPBody:[soapEnvelope dataUsingEncoding:NSUTF8StringEncoding]];

...

但正如蒂姆所说,您必须先通过身份验证才能使用此方法。

有几种身份验证机制,如 Tim 或 NTLM(和其他一些)在上面描述的 Forms 声明。您必须检查您的 Sharepoint 实例的配置方式,或者如果您无法控制服务器的配置,则可能支持多种机制。

于 2013-05-28T16:17:19.257 回答
0

既然你没有发布你的网址,我不得不问,你是在发帖_vti_bin/authentication.asmx吗?

如果您已经是,我只能建议这样做,因为我不知道 ASI 是否正确输入了登录信息。尝试像这样手动将其放入您的 SOAP 消息中:(从这里

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Login xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <username>USERNAME</username>
      <password>PASSWORD</password>
    </Login>
  </soap:Body>
</soap:Envelope>

这应该会返回您使用其余 Web 服务所需的登录 cookie。

编辑:还有一件事,您需要将 SP 服务器设置为使用表单身份验证而不是 NTLM 身份验证(这是默认设置)。

于 2012-12-18T19:52:17.777 回答