尝试在iOS中向URL发送POST请求

时间:2014-10-06 14:27:56

标签: ios objective-c http

我正在尝试发布到iOS中的网络服务器但收到404错误。我正在使用的人能够从一个不同的系统发布地址和数据在一个字符串中:

http://xxx.xxx.xxx.xxx/api/message?salu=mr&firstname=john&lastname=smith&email=smith@testemail.com&country=usa&zipcode=99999&checkbox=y&messagelist=message&q1=1&q2=2&q3=3&q4=4&q5=5

我正在尝试使用Xcode中的以下代码:

 NSString *body = [NSString stringWithFormat:@"message?salu=mr&firstname=john&lastname=smith&email=matt@testmail.com&country=usa&zipcode=99999&checkbox=y&messagelist=message&q1=1&q2=2&q3=3&q4=4&q5=5"];

 NSData *data = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
 NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
 NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api"]];
 [request setHTTPMethod:@"POST"];
 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [request setHTTPBody:data];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

 NSURLResponse *response;
 NSError *err;
 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
 NSString *reply = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSASCIIStringEncoding];
 NSLog(@"requestReply: %@", reply);    

以及NSLog上面的'回复'的最后几行是:

<b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
        <br><br>

        <b> Requested URL: </b>/api<br><br>

</body>

但是,我可以使用以下代码在同一地址上执行'GET',我收到了预期的数据:

 NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] init];
 [request2 setURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api/message"]];
 [request2 setHTTPMethod:@"GET"];

 NSURLResponse *requestResponse;
 NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&requestResponse error:nil];

 NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
 NSLog(@"requestReply: %@", requestReply);

因此,Web服务器正在运行,问题出现在我发布的方式上。有没有人知道我做错了什么?

2 个答案:

答案 0 :(得分:2)

NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api"]];

您是否注意到应该致电NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api/message"]];

/ message是路径的一部分,而不是请求正文。

并期待使用一些现成的框架,例如AFNetworking。

答案 1 :(得分:1)

您已获取请求网址message的最后一个路径组件,并将其放入请求正文中。所以,替换:

NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api"]];

NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xx.xxx.xxx.xxx/api/message"]];

另外,替换:

NSString *body = [NSString stringWithFormat:@"message?salu=mr&firstname=john&lastname=smith&email=matt@testmail.com&country=usa&zipcode=99999&checkbox=y&messagelist=message&q1=1&q2=2&q3=3&q4=4&q5=5"];

使用:

NSString *body = [NSString stringWithFormat:@"salu=mr&firstname=john&lastname=smith&email=matt@testmail.com&country=usa&zipcode=99999&checkbox=y&messagelist=message&q1=1&q2=2&q3=3&q4=4&q5=5"];