苹果原生请求封装
2018-02-10 本文已影响0人
宁梓茞
+(NSMutableURLRequest *)putRequestWithUrl:(NSString *)urlString HTTPMethod:(NSString *)httpMethod HTTPBody:(NSString *)httpBody;
+(NSMutableURLRequest *)putRequestWithUrl:(NSString *)urlString HTTPMethod:(NSString *)httpMethod HTTPBody:(NSString *)httpBody
{
//1.创建URL
NSURL *url = [NSURL URLWithString:urlString];
//2.创建NSURLRequest
// 注意: 如果需要设置请求体或者其他请求参数, 必须使用NSURLRequest的子类
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置模式 // 注意点: 必须大写
request.HTTPMethod = httpMethod;
//设置请求体 // 注意: 只要是POST请求, 系统内部会自动添加?
// post发送中文没有问题
request.HTTPBody = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
// 设置超时时间
request.timeoutInterval = 10;
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Contsetent-Type"];
return request;
}