soap と soap12 の両方を使用しようとしましたが、常にエラーが表示されるようです。私はここで助けを求めて探していて、彼らが私がすべきだと言うように「正確に」やっていますが、私にはうまくいきません. 誰かが私のコードを読むことができれば幸いです。私はobjective-cを始めたばかりなので、詳しく説明してください。ありがとう!
問題点:
- 何も起こらず、すべてが停止します: "Received bytes: 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
そして私の Web サービス: zermattproject.azurewebsites.net/WebService1.asmx?op=Login
なにが問題ですか?問題を修正しようとするたびに、別の問題が発生します。誰でも理由がわかりますか?