zoukankan      html  css  js  c++  java
  • 网络请求之GET、POST请求

    网络请求-GET请求:

    1NSURL 请求地址。


    2NSURLRequest :一个NSURLRequest对象就代表一个请求。它包括的信息有:

    1)一个NSURL对象

    GET请求,不须要写请求头、请求体,仅仅要告诉请求路径和请求參数就能够了。

    2)请求方法

    3)请求超时


    3NSMutableURLRequest : NSURLRequest的子类

    4NSURLConnection:

    负责发送请求,建立client和server的连接。



    NSURLConnection的使用步骤

    1,创建一个NSURL对象,设置请求路径

    // URL里面不能包括中文

    NSURL *url = [NSURL URLWithString:urlStr];


    2,传入NSURL创建一个NSURLRequest对象。设置请求头和请求体

    // 2.2.创建请求对象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //默认就是GET请求

    request.timeoutInterval = 5; //设置请求超时


    3,使用NSURLConnection发送NSURLRequest

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];


    NSURLConnection发送请求

    1。同步请求

    [NSURLConnection sendSynchronousRequest:<#(NSURLRequest *)#> returningResponse:<#(NSURLResponse *__autoreleasing *)#> error:<#(NSError *__autoreleasing *)#>];


    2,异步请求 依据对server返回数据的处理方式的不同,又能够分为2

    1block回调:

    [NSURLConnection sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:<#^(NSURLResponse *response, NSData *data, NSError *connectionError)handler#>];

    2)代理:

    NSURLConnection *conn1 = [[NSURLConnection alloc] initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#>];

    NSURLConnection *conn1 = [[NSURLConnection alloc] initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#> startImmediately:<#(BOOL)#>];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];


    在这样的情况下。须要调用start方法開始发送请求

    - (void)start;

    成为NSURLConnection的代理,最好遵守NSURLConnectionDataDelegate协议;


    POST请求:

    1,设置POST请求

    request.HTTPMethod = @"POST"; // 设置为POST请求

    2,设置请求路径

    // 设置请求路径

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"];

    3。设置请求头

    // 通过请求头告诉serverclient的类型

    [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];

    4,设置请求体

    // 设置请求体

    NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd];

    request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];


    GETPOST请求

    1GET请求在路径后面要加请求參数,POST请求在路径后面不用加请求參数;

    2GET请求不用请求头和请求体,POST请求要请求体(把请求參数转换为请求体),须要设置HTTPMethodHTTPBody


    注意: url中不能写中文,假设非有中文,就须要转码:

    [url stringByAppendingPercentEscapesUsingEncoding:NSUTF8StringEncoding];



    JSON

    什么是JSON

    1,json是一种轻量级的数据格式,一般用于数据交互。

    2。server返回给client的数据,一般都是JSON格式或者XML格式(文件下载除外)

    JSON的格式非常像OC中的字典和数组

    {“name” : "jack", "age" : 10}

    {"names" : ["jack","rose","jim"]}

    标准JSON格式的注意点:key必须用双引號


    JSON解析方案:

    1。在IOS中,JSON的常见解析方式有4

    第三方框架: JSONKit SBJson TouchJSON (性能从左到右,越差)

    苹果原生(自带):NSJSONSerialization (性能最好)


    2NSJSONSerialization的常见方法

    JSON数据 -> OC对象

    [NSJSONSerialization JSONObjectWithData:<#(NSData *)#> options:<#(NSJSONReadingOptions)#> error:<#(NSError *__autoreleasing *)#>];


    OC对象 -> JSON数据

    [NSJSONSerialization dataWithJSONObject:<#(id)#> options:<#(NSJSONWritingOptions)#> error:<#(NSError *__autoreleasing *)#>];


  • 相关阅读:
    Linux上安装Tomcat
    SQLServer2008 关于while循环
    [转]接口和抽象类
    windows 装XP系统
    SQLServer2008 表与表之间的数据导入
    问题消灭机
    报错。。。。。。。。。。
    疑问...........
    SQLServer In和Exists
    struts2 访问一个action的时候出现多次重复访问问题(2次或者3次)
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6897178.html
Copyright © 2011-2022 走看看