ios发送带有POST参数的POST请求

标签 ios post parameters request

我编写了一个 iOS 应用程序,它通过 POST 成功地将字符串上传到服务器。

bodyString = [NSString stringWithFormat:@"text1=%@&submit1=+Absenden+", contentString];

request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.xxx /xxx/get.jsp"]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

我需要更改服务器,所以我使用了相同的代码,但它不起作用,虽然我得到了 200 状态代码。负责新服务器的人说它不起作用,因为我正在发送 POST 但我没有将字符串作为 POST 参数发送。

不幸的是,她不了解 iOS,因此无法向我解释如何操作。我四处搜索,但我什么也没找到,仍然不明白:发送 POST 和同时发送一个字符串作为 POST 参数是什么意思?

任何帮助都会非常友好。

最佳答案

借助 NSURLConnection 委托(delegate)方法处理您的结果数据

  NSString *post = [NSString stringWithFormat:@"postBody=%@",@"Raja"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]];


    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    if( theConnection ){
        // indicator.hidden = NO;
        mutableData = [[NSMutableData alloc]init];
    }

你的 PHP 代码
<?php
    $result = $_POST['postBody'];


    echo $result;
?>

关于ios发送带有POST参数的POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21305931/

相关文章:

php - 如何使用 Python 将 HTTP POST 值发送到 (PHP) 页面?

Python - 自动化 MySQL 查询 : passing parameter

ios - 当 JSON 输入表示 Codable 类型的多个子类时使用 Codable

ios - 自定义 MKAnnotation 按钮

java - 我在哪里向 SOLR 提交 XML

json - 当发布到我的 Django API : errorcom. google.gson.JsonParseException: 无法解析 json

javascript - 使用 JavaScript,如何按照这些参数/规则生成随机 11 个字符字符串? (L,L,L,L/N,L/N,N,N,N,N,N,N)

c# - SqlCommand 参数添加与 AddWithValue

ios - 方向更改后的自定义 UIButton 背景

ios - 将一个 NSObject 替换为另一个并保存在 coredata 中