post get 请求在我们开发中用的很多 我们会经常和服务器打交道
post 请求一般步骤
1.准备工作(规定好了的格式)
2.规定分隔符
3.头的分隔符
4.尾的分隔符
5.封装请求头
6.创建请求
7.对请求操作
8.设置上传数据的格式
9.设置body
10.设置请求体
#import "ViewController.h"
@interface ViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
NSURLConnection *_connection;
NSMutableData *_data;
}
@end
@implementation ViewController
#pragma mark -sendPost2-
//使用二进制 (二进制数据)承载请求信息 用于多文件上传
-(void)sendPost2:(NSString *)urlStr{
NSString *head = @"Content-type: multipart/form-data, boundary=AaB03x\n\n";
NSString *boundary = @"Aab03x";
NSString *headBoundary =[NSString stringWithFormat:@"--%@\n",boundary];
NSString *endBoundary = [NSString stringWithFormat:@"\n--%@--\n",boundary];
NSString *contentString = @"content-disposition: form-data; name=\"pic\";filename=\"start_pressed.png\"Content-Type: image/png\n\n";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlStr];
[request setHTTPMethod:@"POST"];
[request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
NSData *fileData = [NSData dataWithContentsOfFile:[NSBundle mainBundle]pathForResource:@"star_pressed" ofType:@"png"];
NSMutableData *data = [NSMutableData data];
//报文头 = 头 + 头分隔符 + 内容说明 + body 体 + 尾分隔符
[data appendData:[head dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[headBoundary dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[contentString dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:fileData];
[data appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:fileData];
}
#pragma mark - send POST 1-
-(void)sendPost1:(NSString *)urlStr{
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
//规定请求方式为post 请求
[request setHTTPMethod:@"POST"];
NSString *poststr = @"username=Tom&password=Jerry&message=kiss";
//转成二进制数据
NSData *postData = [poststr dataUsingEncoding:NSUTF8StringEncoding];
//设置请求体
[request setHTTPBody:postData];
//设置请求数据的长度
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Connent-Lenght"];
//
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//创建请求等待返回
_connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
#pragma mark -代理方法-
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"接受到响应头");
NSLog(@"response%@",response);
if (_data == nil) {
_data = [[NSMutableData alloc]init];
}
_data.length = 0;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
NSLog(@"收到数据%ld",data.length);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
NSLog(@"接受数据完毕:\n%@",str);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"报错%@",error);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlStr = @"http://localhost/cgi-bin/post1.cgi";
[self sendPost1:urlStr];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ios-post请求
最新推荐文章于 2022-02-18 16:58:52 发布