Ios6 iOS 6-使用POST方法在HTTPBody中发送数据时出错

Ios6 iOS 6-使用POST方法在HTTPBody中发送数据时出错,ios6,http-post,http-status-code-400,Ios6,Http Post,Http Status Code 400,直到昨天晚上,代码还运行得很好,但今天我面临一个奇怪的问题 NSString *hostStr = [@"My API string" stringByAppendingString: @"/users/login"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:hostStr]]; [request setHTTPMethod:

直到昨天晚上,代码还运行得很好,但今天我面临一个奇怪的问题

NSString *hostStr = [@"My API string" stringByAppendingString: @"/users/login"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:hostStr]];
[request setHTTPMethod:@"POST"];

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

NSData *usernameData = [username dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"data[login_name]\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:usernameData]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"data[password]\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:passwordData]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSError *error;
NSURLResponse *response;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *serverOutput = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",serverOutput);
上面的代码给出了以下响应

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache Server at MY API URL Port 80</address>
</body></html>
它给了我以下的请求和响应

{"login_name":"Username or Password is not valid","responseCode":"0"}
意味着当我不在HTTPBody中发送任何数据时,API调用会给我期望的响应。
我不知道这里出了什么问题。非常感谢。好的。这是我从未意识到的问题

当我越来越多地检查我的代码时,我发现“那里”的代码没有任何问题。但问题出在服务器端

我联系了服务器人员,他们说他们启用了一些ModSecurity,因此无法在HTTPBody中发送多部分表单数据

这花了我整整两天的时间才弄明白。希望能节省一些人的时间

{"login_name":"Username or Password is not valid","responseCode":"0"}