gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:09 24 4
gpt4 key购买 nike

我正在构建一个将 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/

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