如何在iphone上使用身份验证解析.net Webservice
本文关键字:身份验证 net Webservice iphone | 更新日期: 2023-09-27 18:03:03
我正在尝试使用nsmutablerrequest和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的示例代码,但我无法通过它。
如果您的问题是由于身份验证,请查看我的问题和答案,因为我必须在那里处理它:iPhone/iPad的SOAP和XML响应解析示例?
除此之外,如果不知道更多的服务期望和你传递的内容,我不知道你能得到多少帮助。现在,我看不出你的代码有什么问题。
编辑:- (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];
}
为了方便访问,我从我对链接问题的回答中编辑了代码。
另外,我正在重读你的代码,你的用户名和密码实际上是"用户名"answers"密码"吗?如果是,并且你复制粘贴了上面的代码,当你在顶部设置变量password时,你拼错了"password"。
-Karoly
如果web服务需要身份验证,那么您需要提供它。
您没有在请求中设置任何身份验证,因此它不会发送任何身份验证。
根据web服务,身份验证可能像basic
一样简单,这只是base64编码的用户名和密码。