如何使用Objective-C将HTTP POST请求发送到PHP服务

用户名

我试图发布到php并获得响应。php文件中有一个回显“ hello”,应仅显示问候。我正在尝试测试发布是否正常,但是在我的错误日志中,NSlog没有显示任何内容:

@interface ViewController ()

@end

@implementation ViewController
@synthesize email, password,receivedData;

-(IBAction)Login:(id)sender{

    // Create the request.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://grouporder.site90.net/test.php"]];

    // Specify that it will be a POST request
    request.HTTPMethod = @"POST";

    // This is how we set header fields
    [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    // Convert your data and set your request's HTTPBody property
    NSString *stringData = [NSString stringWithFormat:@"%@", email];

    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestBodyData;

    // Create url connection and fire request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    //initialize convert the received data to string with UTF8 encoding
    NSString *htmlSTR = [[NSString alloc] initWithData:receivedData
                                              encoding:NSUTF8StringEncoding];
    NSLog(@"%@" , htmlSTR);
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
     NSLog(@"%@" , error);
}
莫哈奇族

由于您没有提及要使用哪种主体类型,因此本示例向您展示了如何发布multipart / form-data请求。

-(NSString *)generateRandomBoundryString {
    CFUUIDRef UUID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef UUIDString = CFUUIDCreateString(kCFAllocatorDefault,UUID);
    NSString *aNSString = (__bridge NSString *)UUIDString;
    CFRelease(UUID);
    CFRelease(UUIDString);
    return aNSString;
}

-(IBAction)Login:(id)sender {
    NSString *bndry = [self generateRandomBoundryString]; 
    NSString *contentType = [[NSString alloc] initWithString:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", bndry]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://grouporder.site90.net/test.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue: contentType forHTTPHeaderField:@"Content-Type"];

    //form-data block
     NSMutableData *requestBody = [NSMutableData data];
    [requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", bndry] dataUsingEncoding:NSUTF8StringEncoding]];
    [requestBody appendData:[[NSString stringWithFormat:@"%@=%@", @"name", @"John"] dataUsingEncoding:NSUTF8StringEncoding]];
    [requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", bndry] dataUsingEncoding:NSUTF8StringEncoding]];
    [requestBody appendData:[[NSString stringWithFormat:@"%@=%@", @"password", @"myPassword"] dataUsingEncoding:NSUTF8StringEncoding]];
    [requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", bndry] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:requestBody];
    //form-data block

    // NSURLConnection Asynchronous Block
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *rspreportStatus, NSData *datareportStatus, NSError *e)
    {
        if (e == nil)
        {
            // If all ok you can processes response data here.
        }
        else {
            NSLog(@"%@", e.localizedDescription);
        }
    }];
}

如果为body application/x-www-form-urlencoded,则应像这样更改Content-Type值。

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

并用//替换// form-data块。

[requestBody appendData:[[NSString stringWithFormat:@"name=%@&password=%@", @"John", @"myPassword"] dataUsingEncoding:NSUTF8StringEncoding]];

@"John"并且@"myPassword"必须经过URL编码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用SAP Cloud SDK版本3.2.0将POST请求发送到OData服务

如何通过Java(Android App)中的POST请求将音频文件发送到服务器?

使用SAP Cloud SDK版本3.2.0将POST请求发送到OData服务时如何避免DestinationAccessException

使用Ajax POST请求将图像和JSON数据发送到服务器?

使用POST方法和HttpURLConnection将Android的JSON对象发送到PHP服务器

无法使用Java将POST请求中的ArrayList发送到REST服务器

使用请求通过http发布将字节数组发送到Web服务

在C#中,如何使用POST方法将数据从SQL Server发送到HTTP请求?

如何使用https-post将node.js的POST请求发送到php页面?

如何使用ClusterIP服务将Lambda的GET / POST请求发送到Pod

如何使用Java将POST请求发送到Octoprint?

如何将POST请求从Android发送到PHP

使用Python将POST发送到PHP

如何使用PHP循环地将发布请求循环发送到远程服务器?

如何将JavaScript输出作为POST请求传递给PHP?[使用js获取TZ,然后发送到mySQL]

如何将请求永久发送到我的http服务器

使用jQuery post或类似的异步JavaScript请求将空数组发送到服务器

无法使用angularjs $ http请求将参数发送到服务器操作方法

如何将Http请求中的JSON数据发送到JSON解析中的POST方法

使用jQuery getJSON将变量发送到php post

使用HTTP Post将机密数据发送到服务器的安全方法

使用POST将js变量从iframe发送到php

如何使用jQuery $ .post()将数组发送到PHP

如何使用http请求将时区信息发送到服务器?

如何将 POST 请求发送到其他服务器绑定文件到 formdata

使用 angular 5 将 POST 数据发送到 php

使用 http POST 将字符串发送到服务器

如何使用 POST 请求(本机反应)将字符串发送到 Web 服务?

使用带有 jQuery 的 POST 请求将数据发送到服务器