-(NSString *)getSoap{
NSString *getEnvelopeData = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n"
"<s:Header>\n"
"<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IHelloWorldService/GetData</a:Action>\n"
"<a:MessageID>urn:uuid:e2c3af4e-5d69-4fd8-94bd-0e0f00976dad</a:MessageID>\n"
"<a:ReplyTo>\n"
"<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>\n"
"</a:ReplyTo>\n"
"</s:Header>\n"
"<s:Body>\n"
"<GetData xmlns=\"http://tempuri.org/\">\n"
"<value>0</value>\n"
"</GetData>\n"
"</s:Body>\n"
"</s:Envelope>\n"];
return getEnvelopeData;
}
-(void)loadSoapRequest{
NSString *soapMessage = [self getSoap];
NSLog(@"soapMessage %@",soapMessage);
NSString *urlString = @"http://l-156024038.testad.com:9000/HelloWorldService.svc";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
[theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest addValue:@"l-156024038.testad.com" forHTTPHeaderField:@"Host"];
[theRequest addValue:@"GetData" forHTTPHeaderField:@"SOAPAction"];
[theRequest setValue:@"Basic " forHTTPHeaderField:@"Authorization"];
// theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:45];
[theRequest setHTTPMethod:@"GET"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
//At this point connection is setup and delegate is set to self, now is time to use connection delegate methods. Four methods that we gonna use:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"response Value %@",response);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"Data Received %@",webData);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Error with connection %@",error);
}
从上面的代码我无法从服务器获得响应!
它总是调用 didFailWithError 委托方法!