iphone - 从 iPhone 使用 POST 到 REST 服务的问题

标签 iphone objective-c rest post

我正在构建一个将 GET 和 POST 到 REST 服务的 iPhone 客户端。 GET 部分工作正常,但我似乎无法发布任何内容。服务器目前只接受 xml。 我尝试过使用 ASIHTTPRequest、ASIFormDataRequest 并自己完成,但似乎没有任何效果。 谁能帮我?我对 Objective-C 编程还很陌生。

我在 didReceiveResponse 中使用以下代码得到一个状态代码:200 作为响应:

NSHTTPURLResponse * httpResponse;      
httpResponse = (NSHTTPURLResponse *) response;
int myStatus = httpResponse.statusCode;
if (myStatus == 400) {
    NSLog(@"Status code: 400");
} else if (myStatus == 200) {
    NSLog(@"Status code: 200");
} else {
    NSLog(@"Other status code");

下面是三种不同的方法...

代码(ASIHTTPRequest):

NSURL *url = [NSURL URLWithString:@"myUrl.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request appendPostData:[@"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]];
    // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];

代码(ASIFormDataRequest):

NSURL *url = [NSURL URLWithString:@"someUrl.com"];
    ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
//       [theRequest setPostValue:@"" forKey:@"key1"];
     [theRequest setPostValue:@"90" forKey:@"key2"];
    [theRequest setPostValue:@"150" forKey:@"key3"];
     // [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    [theRequest startAsynchronous];

代码(第三种方法):

NSData *myPostData = [[NSString stringWithFormat:@"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
//      NSString *xmlPostData = @"<my xml>";
     NSURL *theURL = [NSURL URLWithString:@"someUrl.com"];

     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];

     [theRequest setHTTPMethod:@"POST"];
     [theRequest setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
//       [theRequest setValue:@"UTF-8" forHTTPHeaderField:@"Charset"];
    [theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// myPostData];

     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];

所有这些 -(void)connection: 样式函数怎么样?它们中的任何一个都必须包含特定代码才能使连接正常工作吗?

非常感谢您的帮助!

最佳答案

NSURL *url = [NSURL URLWithString:@"http://myURL.com"];

ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *myPostData = [[NSString stringWithFormat:@"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];

[request addRequestHeader:@"Content-Type" value:@"application/xml"];
[request setPostBody:myMutablePostData];
[request setDelegate:self];
[request startSynchronous];

关于iphone - 从 iPhone 使用 POST 到 REST 服务的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4390006/

相关文章:

ios - 在开发设备上安装旧版 iPhone 操作系统

iphone - 如何直接保存到持久存储,而不是将数据保存到内存中

ios - RESTKit异常: No mappable object representations were found at the key paths searched

ios - 如何使用自动布局从 IB 访问正确的宽度值

ios - 如何使用电话号码在地址簿中搜索 ios

spring - 如何在 Spring Boot 应用程序中关闭 Spring Security

iphone - 无法从 NSXMLParser 解码 UTF8 字符

ios - 如何取消选择选定的 UITableView 单元格?

java - Jersey @Path 用于同一类中的复数/单个 REST 名词

java - 使用 Java Rally Rest API 在 rally 中将测试用例添加到 TestSets