我在IOS中有一个Web服务,我提供了ip地址,它将返回该国家/地区。代码是:
表示.m:
#import "ViewController.h"
@implementation ViewController
@synthesize ipAddress;
@synthesize activityIndicator;
- (IBAction)btnFindCountry:(id)sender {
NSString *soapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<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/\">"
"<soap:Body>"
"<FindCountryAsXml xmlns=\"http://www.ecubicle.net/webservices/\">"
"<V4IPAddress>%@</V4IPAddress>"
"</FindCountryAsXml>"
"</soap:Body>"
"</soap:Envelope>", ipAddress.text];
//---print it to the Debugger Console for verification---
NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:
@"http://www.ecubicle.net/iptocountry.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d",
[soapMsg length]];
[req addValue:@"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://www.ecubicle.net/webservices/FindCountryAsXml"
forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//---start animating —
[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req
delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
}
-(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 {
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc]
initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(@"%@",theXML);
[theXML release];
//---stop animating---
[activityIndicator stopAnimating];
[connection release];
[webData release];
}
- (void)dealloc {
[ipAddress release];
[activityIndicator release];
[super dealloc];
}
@end
和.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UITextField *ipAddress;
IBOutlet UIActivityIndicatorView *activityIndicator;
NSMutableData *webData;
NSMutableString *soapResults;
NSURLConnection *conn;
}
@property (nonatomic, retain) UITextField *ipAddress;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
-(IBAction) btnFindCountry:(id)sender;
@end
当我运行它并输入3.4.5.6时,调试器中的结果是:
WebService[3525:60b] <?xml version="1.0" encoding="utf-8"?><soap:Envelop
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/"><soap:Body><FindCountryAsXml
xmlns="http://www.ecubicle.net/webservices/"><V4IPAddress>3.4.5.6</V4IPAddress>
</FindCountryAsXml></soap:Body></soap:Envelope>
表示请求已成功发送且Web服务没有响应
的教程我应该得到的回应是:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<FindCountryAsXmlResponse
xmlns="http://www.ecubicle.net/webservices/">
<FindCountryAsXmlResult>
<IPCountryService xmlns="">
<Country>United States</Country>
</IPCountryService>
</FindCountryAsXmlResult>
</FindCountryAsXmlResponse>
</soap:Body>
</soap:Envelope>
问题是什么? 有人可以帮忙吗?