首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将XML数据从iphone发布到web服务器

将XML数据从iphone发布到web服务器
EN

Stack Overflow用户
提问于 2011-02-07 13:03:28
回答 2查看 8.4K关注 0票数 3

我想用以下格式发布xml数据:

代码语言:javascript
运行
复制
  <?xml version="1.0" encoding="UTF-8"?>    
                       <data>                                               
                         <email>xyz@domain.com</email>
                                 <password>xyz123</password>                                      
                       </data>

并以以下格式从the服务器接收

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>    
            <user>
                          <user_id>12</user_id> 

                 </user>

现在我正在尝试使用NSUrlConnection和NSMutableRequest,我可以像表单post一样在名称上发布数据,但我只想发布xml数据。我也尝试过使用ASIHTTPRequest。任何代码或链接都是高度精准的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-07 16:39:14

最后,我使用了来自http://allseeing-i.com/ASIHTTPRequest/的库

并从以下代码发布:

代码语言:javascript
运行
复制
NSURL *url = [NSURL URLWithString:@"http://url to post data"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request addPostValue:@"test name" forKey:@"name"];

    [request setDelegate:self];
    [request startSynchronous];

并使用委托方法接收数据或错误。

代码语言:javascript
运行
复制
- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    NSLog(@"%@",responseString);

    // Use when fetching binary data
    NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"Error %@",error);
}
票数 2
EN

Stack Overflow用户

发布于 2011-02-07 14:47:20

代码语言:javascript
运行
复制
//prepare request
NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result); 
//here you get the response 
}

当然,您也可以使用异步请求。然后你必须实现委托。

编辑。您还可以尝试执行以下操作:

代码语言:javascript
运行
复制
 NSString* boundary = @"---------------------------14737809831466499882746641449";

 NSMutableData* postbody = [NSMutableData dataWithCapacity: 200];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", _boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"content.xml\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: text/xml\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@\r\n", val] dataUsingEncoding: NSUTF8StringEncoding]];

NSMutableURLRequest* requestURL= [[[NSMutableURLRequest alloc] init] autorelease];
[requestURL setURL:[NSURL URLWithString: _request.text]];
[requestURL setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[requestURL addValue:contentType forHTTPHeaderField: @"Content-Type"];
[requestURL addValue: [NSString stringWithFormat:@"%d",[postbody length]] forHTTPHeaderField: @"Content-length"]; 

[requestURL setHTTPBody: postbody];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4918205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档