NSMutableURLRequest以及请求中含中文的处理方式

Posted on 2016-07-19 21:54  柠檬片  阅读(459)  评论(0)    收藏  举报
  • NSMutableURLRequest是NSURLRequest的子类,常用方法有
    设置请求超时等待时间(超过这个时间就算超时,请求失败)

    - (void)setTimeoutInterval:(NSTimeInterval)seconds;

  
    设置请求方法(比如GET和POST)

    - (void)setHTTPMethod:(NSString *)method;

 

    设置请求体

    - (void)setHTTPBody:(NSData *)data;

 

    设置请求头

    - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

 

  • 创建GET请求

    NSString *urlStr = [@"http://120.25.226.186:32812/login2?username=蜗牛&pwd=6666"         

    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlStr];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

 

  • 创建POST请求

    NSString *urlStr = @"http://120.25.226.186:32812/login";

    NSURL *url = [NSURL URLWithString:urlStr];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    request.HTTPMethod = @"POST";

    // 请求体

    NSString *bodyStr = @"username=sxf333&pwd=33333";

    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];