iPhone中WebService的使用
使用的WebService是:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
其中的getWeatherByCityName函数
常使用的技术有:SOAP,HTTP GET,HTTP POST三种方式
(一)SOAP(简单对象访问协议)方式
当你使用SOAP时,必须用到POST方式
(1)SOAP 1.1
以下是请求实例:
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"
<?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>
<getWeatherbyCityName xmlns="http://WebXml.com.cn/">
<theCityName>string</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>
例如:
-(void) buttonClicked:(id) sender{
NSString *province=[NSStringstringWithFormat:@"上海"];
//设置soap请求信息
NSString *soapString=[[NSStringalloc] initWithFormat:@"<?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>"
"<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
"<theCityName>%@</theCityName>"
"</getWeatherbyCityName>"
"</soap:Body>"
"</soap:Envelope>",province];
NSLog(@"%@",soapString);
//soap请求地址
NSURL *url=[[NSURLalloc] initWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"];
//请求
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
//设置请求头部
////设置ContentType
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
////设置SOAPAction
[request addValue:@"http://WebXml.com.cn/getWeatherbyCityName" forHTTPHeaderField:@"SOAPAction"];
//设置Content-length
[request addValue:[NSStringstringWithFormat:@"%d",[soapString length]] forHTTPHeaderField:@"Content-Length"];
//设置请求类型 POST或GET
[request setHTTPMethod:@"POST"];
//设置请求Body(只有post方式有)
[request setHTTPBody:[soapString dataUsingEncoding:NSUTF8StringEncoding]];
//连接
NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
webData=[NSMutableData data];
}
}
_________ViewController文件_____________
#import <UIKit/UIKit.h>
//注意使用的协议
@interface WebServiceViewController : UIViewController<NSURLConnectionDelegate,NSURLConnectionDataDelegate>{
NSMutableData *webData;
UIWebView *myWebView;
UITextField *textFiled;
}
@property (strong, nonatomic) NSMutableData *webData;
@property (strong, nonatomic) UIWebView *myWebView;
@property (strong, nonatomic) UITextField *textFiled;
-(void) buttonClicked:(id) sender;
@end
---------NSURLConnectionDelegate,NSURLConnectionDataDelegate实现方法---------
//接收相应的时候触发
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength:0];
}
//接收数据的时候触发
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
//全部完成时触发
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *dataString=[[NSStringalloc] initWithBytes:[webDatabytes] length:[webDatalength] encoding:NSUTF8StringEncoding];
NSLog(@"%@",dataString);
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Error:%@",error);
}
(2)SOAP 1.2 (除了soap不一样外,其他都一样。)
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getWeatherbyCityName xmlns="http://WebXml.com.cn/">
<theCityName>string</theCityName>
</getWeatherbyCityName>
</soap12:Body>
</soap12:Envelope>
(二)POST方式
POST /WebServices/WeatherWebService.asmx/getWeatherbyCityName HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length
theCityName=string
例如:
-(void) buttonClicked:(id) sender{
NSString *postString=@"theCityName=上海";
//此处的URL是POST /WebServices/WeatherWebService.asmx/getWeatherbyCityName 见上!
NSURL *url=[NSURL URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"];
NSMutableURLRequest *request=[NSMutableURLRequestrequestWithURL:url];
[request addValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];//注意是中划线
[request addValue:[NSStringstringWithFormat:@"%d",[postString length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[NSURLConnectionconnectionWithRequest:request delegate:self];
if (connection) {
webData=[NSMutableDatadata];
}
}
其他一样;