ios - 从 IOS NSMutableURLRequest 发送数据时无法将数据作为 Post 获取

标签 ios iphone objective-c nsurlconnection nsurlrequest

这是我的代码片段,我将代码发送到 PHP 页面,但是当我执行 print_r($_POST); 时我得到空数组,但是当我执行 print_r($_GET) 时,我得到了我用来发布数据的变量,即 name 但它也是空的,任何 1 都可以解决我在做什么这里错了

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnFetchData1:(id)sender {
//    NSString *urlString = [NSString stringWithFormat:@"http://localhost/adi/adnan.php?name=%@", [self.txtName text]];
      NSString *urlString = [NSString stringWithFormat:@"http://localhost/adi/adnan.php"];
     NSString *post = [NSString stringWithFormat:@"name=%@",@"adnan"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
delegate:self];
    if (conn) {
        _receivedData=[NSMutableData data];
    } else {
        //something bad happened
    }
}

#pragma NSUrlConnectionDelegate Methods

-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
    if (_receivedData == NULL) {
        _receivedData = [[NSMutableData alloc] init];
    }
    [_receivedData setLength:0];
    NSLog(@"didReceiveResponse: responseData length:(%d)", _receivedData.length);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_receivedData appendData:data];
}


- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error {
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);

    NSString *responseText = [[NSString alloc] initWithData:_receivedData encoding: NSASCIIStringEncoding];
    NSLog(@"Response: %@", responseText);

    NSString *newLineStr = @"\n";
    responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];

    [self.lblData setText:responseText];
}

最佳答案

你创建了 postLength 但从未使用过它,试试这个它可能会解决它:

//create URL for the request
NSString *urlString = [NSString stringWithFormat:@"http://localhost/adi/adnan.php"];

//Post data
NSString *post = [NSString stringWithFormat:@"name=%@",@"adnan"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

//the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Bind the request with Post data
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

关于ios - 从 IOS NSMutableURLRequest 发送数据时无法将数据作为 Post 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430715/

相关文章:

ios - Xcode 6、ios 8 模拟器无法启动

ios - 无法在两个 Apple 开发者帐户之间转移应用程序

android - 将自定义日历与 Android 和 iOS 同步

iPhone:导航栏返回导航项目onClick-event?

ios - 收据验证不正确

ios - UIStackView ios版本之间不同的显示隐藏动画

ios - 连续多个 NSURLConnection

iphone - 请将此设备连接到 Xcode 以安装开发支持文件

ios - 如何使用连接 USB 线调试在移动设备中执行的 iOS 应用程序(不是由 Xcode 启动)

iphone - xcode 将警告显示为错误