iOS http post 网络

http post //亲测可用
 
 
//(不推荐)方法1:发送一个同步请求,当网络不顺畅的时候会出现阻塞。主线程卡死在那了。
NSString * URLString = @"http://zju.ngrok.joyingtec.com:8000/curtain.php";
    NSURL * URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSString * postString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<root>"
    "<command_id>1</command_id>"
    "<command_type>execute</command_type>"
    "<id>123</id>"
    "<action>open</action>"
    "<value>1<value>"
    "</root>";
    
    NSData * postData = [postString dataUsingEncoding:NSUTF8StringEncoding];  //将请求参数字符串转成NSData类型
    
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc]init];
    [request setHTTPMethod:@"post"]; //指定请求方式
    [request setURL:URL]; //设置请求的地址
    [request setHTTPBody:postData];  //设置请求的参数
    
    NSURLResponse * response;
    NSError * error;
    NSData * backData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    if (error)
    {
        NSLog(@"error : %@",[error localizedDescription]);
    }
    else
    {
        NSLog(@"backData : %@",[[NSString alloc]initWithData:backData encoding:NSUTF8StringEncoding]);
    }
 
 
 
 
 
 
 
 
//(推荐)方法2:
<pre name="code" class="objc">//创建url
    NSURL *url=[NSURL URLWithString:@"http://zju.ngrok.joyingtec.com:8000/curtain.php"];
    
//创建请求
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    
    [request setHTTPMethod:@"POST"];
    
    NSString *str = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<root>"
    "<command_id>1</command_id>"
    "<command_type>execute</command_type>"
    "<id>123</id>"
    "<action>open</action>"
    "<value>1<value>"
    "</root>";
    
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:data];
    
    //连接服务器
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
    {
        //data就是从网络返回的数据
        //对data处理
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",string);
        
        //重新回到主线程
        dispatch_async(dispatch_get_main_queue(), ^
        {
            //更新UI
            NSLog(@"回来主线程更新UI");
           
        });
    }];


 
 
 
 
 
 
 
 
 
 

//方法3:这里要实现代理了(NSURLConnectionDataDelegate)

//创建url
    NSURL *url=[NSURL URLWithString:@"http://zju.ngrok.joyingtec.com:8000/curtain.php"];
    
    //创建请求
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    
    [request setHTTPMethod:@"POST"];
    
    NSString *str = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<root>"
    "<command_id>1</command_id>"
    "<command_type>execute</command_type>"
    "<id>123</id>"
    "<action>open</action>"
    "<value>1<value>"
    "</root>";
    
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:data];
    
    self.connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(self.connection)
    {
        self.receiveData=[NSMutableData data];
    }

//要实现的代理方法

//接受到服务器回应的时候调用次方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"开始接受");
    [self.receiveData setLength:0];
}

//接受到服务器传输数据的时候调用,此方法根据数据大小执行若干次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}
//数据传完之后调用此方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *receiveStr=[[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding];
    NSLog(@"====%@",receiveStr);
    NSLog(@"接受完成了哦");
}

//网络请求中出现错误(断网,连接超时等)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}


你可能感兴趣的:(iOS http post 网络)