Obj-C & webservice

1 直接发送http请求,自己解析http请求方法

 

 

 

 

- (IBAction) getData{ // hide the keyboard [cityName resignFirstResponder]; [countryName resignFirstResponder]; 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>" "<GetWeather xmlns=/"http://www.webserviceX.NET/">" "<CityName>%@</CityName>" "<CountryName>%@</CountryName>" "</GetWeather>" "</soap:Body>" "</soap:Envelope>", cityName.text, countryName.text ]; //---print it to the Debugger Console for verification--- NSLog(soapMsg); NSURL *url = [NSURL URLWithString: @"http://www.webservicex.net/globalweather.asmx"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; //---set the headers--- /* POST /globalweather.asmx HTTP/1.1 Host: www.webservicex.net Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://www.webserviceX.NET/GetWeather" */ NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:@"http://www.webserviceX.NET/GetWeather" forHTTPHeaderField:@"SOAPAction"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; //---set the HTTP method and body--- [req setHTTPMethod:@"POST"]; [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; [activityIndicator startAnimating]; conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if (conn) { webData = [[NSMutableData data] retain]; } }  

 

 

你可能感兴趣的:(Obj-C & webservice)