0
我一直在嘗試使用soap和soap12,但錯誤似乎總是出現。我一直在這裏尋找一些幫助,我確實「像他們說我應該這樣做,但它不適合我。我想如果有人可以閱讀我的代碼。我剛開始使用Objective-C,請詳細解釋。謝謝!不能使用我的web服務從目標c
的問題:
- 什麼也沒有發生,一切都停止在「接收字節數:0
- 因爲媒體我不支持的
我的代碼看起來像這樣的服務器不能服務請求:
#import "ViewController.h"
@interface ViewController() {
}
@end
@implementation ViewController
@synthesize username, password, conWebData, soapResults, xmlParser;
- (IBAction)Login:(UIButton *)sender {
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:Body>\n"
"<Login xmlns=\"http://zermattproject.azurewebsites.net/WebService1.asmx\">\n"
"<userName>%@</userName>\n"
"<password>%@</password\n"
"</Login>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", username.text, password.text];
soapMessage = [NSString stringWithFormat:soapMessage, username.text];
soapMessage = [NSString stringWithFormat:soapMessage, password.text];
NSData *envelope = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *url = @"http://zermattproject.azurewebsites.net/WebService1.asmx";
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
/*[theRequest addValue:@"http://zermattproject.azurewebsites.net/WebService1.asmx/Login" forHTTPHeaderField:@"SOAPAction"];*/
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:envelope];
[theRequest setValue:@"text/xml; charset=utf-8; action=http://zermattproject.azurewebsites.net/WebService1.asmx?op=Login" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [envelope length]] forHTTPHeaderField:@"Content-Length"];
NSLog(@"%@", soapMessage);
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if(theConnection)
{
conWebData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
[conWebData setLength:0];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data
{
[conWebData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConnection"), error.localizedDescription,
[error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSLog(@"DONE. Received Bytes: %d", [conWebData length]);
NSString *theXML = [[NSString alloc]initWithBytes:[conWebData mutableBytes] length:[conWebData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@", theXML);
xmlParser = [[NSXMLParser alloc]initWithData: conWebData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc]init];
}
recordResults = TRUE;
}
- (void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string
{
if(recordResults)
{
[soapResults appendString: string];
}
}
- (void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"LoginResult"])
{
recordResults = FALSE;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:soapResults delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
soapResults = nil;
}
}
@end
而我的網絡服務:zermattproject.azurewebsites.net/Web Service1.asmx?op =登錄
什麼是錯?每次嘗試解決問題時都會出現另一個問題。任何人都能看到爲什麼
謝謝!我現在沒有得到任何錯誤,但我只是得到了soapMessage和「Response:」的控制檯 – user2544317