我尝试使用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的解决方案,但没有运气:
答案 0 :(得分:1)
确定。我解决了 那是因为我将url的结尾设置为文件夹/但执行文件位于文件夹/ index.php中。 我得到301重定向我的帖子请求,只得到第二个请求的结果为GET。