【问题标题】:how to pass the custom headers and get the data from the server using post method (JSON)如何使用 post 方法 (JSON) 传递自定义标头并从服务器获取数据
【发布时间】:2016-02-22 21:33:03
【问题描述】:

在下面我分享了一些 PHP 代码,我在传递自定义标头并在 Objective-c 中创建 $message 和 $header 时感到震惊。所以请帮助我。
enter image description here

【问题讨论】:

    标签: ios objective-c iphone ios7 ios9


    【解决方案1】:

    根据你的图片

     POST for posting data to SERVER  
    
    -(void)postDataToServer
    {
      //STEP 1 :Create the MutableURL Request
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.rgsgapp.com/api/debug/show"]];
    
    
     //STEP 2 :create the Method "POST"
     [request setHTTPMethod:@"POST"];
    
     //STEP 3 :Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
     NSString *userUpdate =[NSString strin gWithFormat:@"API-ID=%@&API-TIME=%@API-HASH=%@",txtFldID.text,txtFldTime.text,txtFldHash.text,nil]; //Here you can give string or textField value
    
     //STEP 4 :Check The Value what we passed
     NSLog(@"the data Details is =%@", userUpdate);
    
     //STEP 5 :Convert the String to Data
     NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
    
     //STEP 6 :Apply the data to the body
     [request setHTTPBody:data1];
    
     //STEP 7 :Create the response and Error
     NSError *err;
     NSURLResponse *response;
    
     //STEP 8 :send synchronous request to server  
     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    
     //STEP 9 :getting the response 
     NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
    
     //STEP 10:Check whether we got the Response or Not
     NSLog(@"got response==%@", resSrt);
     if(resSrt)
     {
       NSLog(@"got response");
     }
     else
     {
       NSLog(@"faield to connect");
     }
    }
    

    在您的操作方法或您想要做的地方调用上述方法。

    【讨论】:

    • 请勾选我的回答,因为它对其他人有帮助,如果他们看到您的问题。
    • 以上是发布数据到服务器的非常基本的同步方法。
    • 好吧,我会帮你的。你从我的回答中明白了什么?
    • 请逐步完成我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多