如何使用身份验证在iphone上解析.Net Web服务

时间:2011-07-21 06:13:52

标签: c# asp.net objective-c ios4 iphone

我正在尝试使用NSMutableRequest和NSURL连接获取xml,但是我收到了0个字节,但是当我在链接中传递了WSDL,所以在响应中获取模式,但是我想获取xml然后将使用TouchXML进行解析。我的代码如下。

    NSString *username = @"username";
    NSString *password = @"pasword";
    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<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/\">\n"
                             "<soap:Header>\n"
                             "<RequestAuthenticator xmlns=\"http://tempuri.org/\">\n"
                             "<Password>%@</Password>\n"
                             "<UserName>%@</UserName>\n"
                             "</RequestAuthenticator>\n"
                             "</soap:Header>\n"
                             "<soap:Body>\n"
                             "<METHODNAME xmlns=\"http://tempuri.org/\"/>\n"
                             "</soap:Body>\n</soap:Envelope>\n",password,username];
    NSLog(@"%@",soapMessage);

     NSURL *url = [NSURL URLWithString:@"http://websitename.com/website_english/wservice/website_service.asmx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    /*ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setUseKeychainPersistence:YES];
    [request setUsername:@"username"];
    [request setPassword:@"password"];
    [request setDelegate:self];
    [request startAsynchronous];*/
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue: @"http://tempuri.org/METHODNAME" forHTTPHeaderField:@"SOAPAction"];
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
    [theXML release];
}

但它返回0字节而没有xml,原始网站名称不会透露安全性。

我也尝试了HelloSOAP.xcodeproj示例代码,但我无法通过它。

2 个答案:

答案 0 :(得分:0)

如果Web服务需要身份验证,则需要提供身份验证。

您未在请求中设置任何身份验证,因此不会发送任何身份验证。

根据Web服务的不同,身份验证可能与basic一样简单,只是用户名和密码base64编码。

答案 1 :(得分:0)

如果你的问题是由于身份验证而不是看看我的问题和答案,因为我必须在那里处理:SOAP and XML Response Parsing Samples for iPhone/iPad?

除此之外,如果不了解服务的期望和通过的内容,我不知道你能获得多少帮助。就像现在一样,我没有看到你的代码有什么问题。

编辑:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLCredential *myCreds = [[NSURLCredential alloc] initWithUser:@"**USERNAME**" password:@"**PASSWORD**" persistence:NO];

    [challenge.sender useCredential:myCreds forAuthenticationChallenge:challenge];
    [myCreds release];
}

我在代码中编辑了我对链接问题的回答,以便于访问。

另外,我正在重读您的代码,实际上是您的用户名和密码,“用户名”和“密码”?如果是这样,并且您复制并粘贴了上述代码,当您在顶部附近设置可变密码时,您拼错了“密码”。

-Karoly