iOS—请求Web Service

1、iOS SDK 同步GET请求

  NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.51work6.com/service/mynotes/[email protected]&type=JSON&action=query"];
   strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  NSURL *url = [NSURL URLWithString:strURL];

  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    NSData *data  = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSLog(@"请求完成...");
    NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"resDict:%@",resDict);

2、iOS SDK 异步GET请求

/*
 * 开始请求Web Service
 */
-(void)startRequest
{

    NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.weather.com.cn/adat/sk/101010100.html"];
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

	NSURL *url = [NSURL URLWithString:strURL];

	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    NSURLConnection *connection = [[NSURLConnection alloc]
                                   initWithRequest:request
                                   delegate:self];
    if (connection) {
        self.datas = [NSMutableData new];
    }
}

#pragma mark- NSURLConnection 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.datas appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {

    NSLog(@"%@",[error localizedDescription]);
}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
    NSLog(@"请求完成...");
    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:self.datas options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"dict:%@",dict);
}

iOS SDK POST请求

/*
 * 开始请求Web Service
 */
-(void)startRequest
{

    NSString *strURL = @"http://www.51work6.com/service/mynotes/WebService.php";
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:strURL];

    NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@", @"[email protected]",@"JSON",@"query"];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];

    NSURLConnection *connection = [[NSURLConnection alloc]
                                   initWithRequest:request
                                   delegate:self];
    if (connection) {
        self.datas = [NSMutableData new];
    }
}

#pragma mark- NSURLConnection 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.datas appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {

    NSLog(@"%@",[error localizedDescription]);
}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
    NSLog(@"请求完成...");
    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:self.datas options:NSJSONReadingAllowFragments error:nil];
}

GET方法是向指定资源发出请求,只用在读取数据。POST方法是向指定资源提交数据,请求服务器进行处理

同步请求是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式。  
异步请求是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通讯方式。

MKNetworkKit框架 GET请求

/*
 * 开始请求Web Service
 */
-(void)startRequest
{
    NSDictionary *[email protected]{@"format": @"2",@"cityname": @"南京",@"key": @"1174d1a31d33b1dacb69d15c7756f898"};

    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"v.juhe.cn" customHeaderFields:nil];   //HostName不能加HTTP://
    MKNetworkOperation *op = [engine operationWithPath:@"/weather/index" params:parameter httpMethod:@"GET" ssl:NO];

    [op addCompletionHandler:^(MKNetworkOperation *operation) {

        NSLog(@"responseData : %@", [operation responseString]);  //显示中文,但输出无格式
        NSData *data  = [operation responseData];
        NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        NSLog(@"数据:%@",resDict);    //不显示中文,但输出有格式

    } errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork请求错误 : %@", [err localizedDescription]);
    }];
    [engine enqueueOperation:op];

}

MKNetworkKit框架  POST请求    待修改

/*
 * 开始请求Web Service
 */
-(void)startRequest
{
    NSString *path = [[NSString alloc] initWithFormat:@"/weather/index"];

    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    [param setValue:@"2" forKey:@"format"];
    [param setValue:@"苏州" forKey:@"cityname"];
    [param setValue:@"1174d1a31d33b1dacb69d15c7756f898" forKey:@"key"];

    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"v.juhe.cn" customHeaderFields:nil];
    MKNetworkOperation *op = [engine operationWithPath:path params:param httpMethod:@"POST"];

    [op addCompletionHandler:^(MKNetworkOperation *operation) {

        NSLog(@"responseData : %@", [operation responseString]);
        NSData *data  = [operation responseData];
        NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        [self reloadView:resDict];

    } errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork请求错误 : %@", [err localizedDescription]);
    }];
    [engine enqueueOperation:op];

}

  

 

时间: 2024-12-21 13:11:09

iOS—请求Web Service的相关文章

iOS.访问 Web Service.同步GET请求方法

1.字符串转换为URL字符串NSString分类 #import <Foundation/Foundation.h> @interface NSString (URLEncoding) -(NSString *)URLEncodedString; -(NSString *)URLDecodedString; @end #import "T20140628013418NSString+URLEncoding.h" @implementation NSString (URLEn

iOS.访问 Web Service.异步GET请求方法

#import <UIKit/UIKit.h> #import "T20140628024750NSNumber+Message.h" #import "T20140628024750NSString+URLEncoding.h" @interface T20140628024750ViewController : UITableViewController<NSURLConnectionDelegate> @property (nonato

iOS.访问 Web Service.异步POST请求方法

#import <UIKit/UIKit.h> #import "T20140628024917NSNumber+Message.h" #import "T20140628024917NSString+URLEncoding.h" @interface T20140628024917ViewController : UITableViewController<NSURLConnectionDelegate> @property (nonato

iOS 中web service数据请求

Web Service也叫XML Web Service WebService是一种可以接收从Internet或者其它系统中传递过来的请求,轻量级的独立的通讯技术. #import <UIKit/UIKit.h> //遵循NSURLConnectionDataDelegate协议 @interface ViewController : UIViewController<NSURLConnectionDataDelegate> //接受从服务器返回的数据 @property(stro

iOS.访问 Web Service.MKNetworkKit_POST

#import <UIKit/UIKit.h> #import "T20140628025249NSNumber+Message.h" #import "T20140628025249NSString+URLEncoding.h" #import "MKNetworkEngine.h" #import "MKNetworkOperation.h" @interface T20140628025249ViewCont

iOS.访问 Web Service.MKNetworkKit_GET

#import <UIKit/UIKit.h> #import "T20140628025200NSNumber+Message.h" #import "T20140628025200NSString+URLEncoding.h" #import "MKNetworkEngine.h" #import "MKNetworkOperation.h" @interface T20140628025200ViewCont

iOS.访问 Web Service.使用下拉刷新控件

#import <UIKit/UIKit.h> #import "T20140628025702NSNumber+Message.h" #import "T20140628025702NSString+URLEncoding.h" @interface T20140628025702ViewController : UITableViewController @property (nonatomic,strong) NSMutableArray *lis

C++请求web service与xml解析

1. C++解析XML的开源库 在项目中XML的解析使用的是开源的第三方库,TinyXML:这个解析库的模型通过XML文件,然后再内存中生成DOM模型,从而让我们可以很方便的遍历这颗XML树. DOM模型即文档对象模型,是将整个文档分成多个元素(如:书.章.节.段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系.先看一下TinyXML中的主要类和XML文档之间的对应关系,下图是TinyXML中主要class的类图,反应各个类之间的静态关系. TiXmlBase是所有类的基类,TiX

C#代码 请求web service soap类型服务(wsdl文件)

1.  首先搞到wsdl文件(客户提供的地址下载http://10.48.36.6:8080/ShuJuHuanJing.asmx?WSDL)网站中打开,复制到txt文件中存储为wsdlModel.wsdl.注意后缀. 2. 打开VS新建一个cs类用于存储转换wsdl文件 3. 在程序中打开VS中的命令行 4.  从wsdl路径下载至cs文件   输入命令:wsdl /language:c# /n:CHEER.PresentationLayer /out:文件存储路径 http://10.48.