gpt4 book ai didi

ios - 设置 HTTP 方法 : @"POST" not working

转载 作者:行者123 更新时间:2023-11-29 12:41:07 24 4
gpt4 key购买 nike

我尝试了 stackoverflow 的许多解决方案来解决这个问题,但没有成功。我的问题是 setHTTPMethod 不起作用,因为它总是使用 GET 方法。我的代码是:

NSHTTPURLResponse *response;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAPIUrl]];
NSMutableData *requestBody = [NSMutableData data];
NSString *boundary = @"ljlkjlkkljlk98jjmnj";

[request setHTTPMethod:@"POST"];
[request setCachePolicy: NSURLRequestReloadIgnoringCacheData];

[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField: @"Content-Type"];

[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"file",@"file.gz"] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[self gzipData:returnData]];

NSMutableArray *keys=[[NSMutableArray alloc] init];
NSMutableArray *vals=[[NSMutableArray alloc] init];
//post vars
[keys addObject:@"postvariable"];
[vals addObject:@"kjasdhfklsadjfhsajdkfhasdfjhsadfkjsadfhskdfjhjhf"];
//add post vars to body

for(i=0;i<[keys count];i++)
{
[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@\"; \r\n\r\n%@",boundary,
[keys objectAtIndex:i],
[vals objectAtIndex:i]
] dataUsingEncoding:NSUTF8StringEncoding]];
}
//add post vars to body
[requestBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:requestBody];

NSData *answer = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: nil ];

NSLog(@"server answer:%@",[[NSString alloc] initWithData:answer encoding:NSUTF8StringEncoding]);

服务器上的 PHP

if(isset($_REQUEST["getjob"]))
{

}
else
{
foreach ($_SERVER as $name => $value) {
echo "$name: $value\n";
}

var_dump($_FILES);
var_dump($_POST);
echo file_exists($_FILES["file"]["tmp_name"]);
echo "ok";
}

我有什么:

REMOTE_PORT: 57688
GATEWAY_INTERFACE: CGI/1.1
SERVER_PROTOCOL: HTTP/1.1
REQUEST_METHOD: GET
QUERY_STRING:
REQUEST_URI: /dcapi/

那是服务器回答只返回 $_SERVER 变量。我总是得到 GET 方法。但我总是使用 POST。

我尝试了很多来自 stackoverflow 的解决方案,但没有成功:

  1. http://kemal.co/index.php/2012/02/fetching-data-with-getpost-methods-by-using-nsurlconnection/ - 我只是下载资源。没有结果。

  2. HTTP Post Request in Objective-C Not Working行不通。

  3. Upload file not working vie HTTP Post行不通。

  4. POST from iPhone to PhP not working行不通。

最佳答案

好的。我解决它。那是因为我将 url 的结尾设置为 folder/但执行文件在 folder/index.php 中。我为我的发布请求获得 301 重定向,并且仅从第二个请求获取结果作为 GET。

关于ios - 设置 HTTP 方法 : @"POST" not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24900415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com