从iPhone向Web服务器发布XML数据

3
我希望以以下格式发布XML数据:
  <?xml version="1.0" encoding="UTF-8"?>    
                       <data>                                               
                         <email>xyz@domain.com</email>
                                 <password>xyz123</password>                                      
                       </data>

从Web服务器接收以下格式:
<?xml version="1.0" encoding="UTF-8"?>    
            <user>
                          <user_id>12</user_id> 

                 </user>

感谢您的求助!现在我正尝试使用NSUrlConnection和NSMutableRequest。我可以像表单提交一样发布名称数据,但我想发布XML数据。我还尝试过使用ASIHTTPRequest。非常感谢提供任何代码或链接。

2个回答

4
//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 
}

当然,您也可以使用异步请求。然后您需要实现委托(delegate)。
编辑。您还可以尝试这个:
 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];

我正在将数据发送到 PHP 文件,在打印 print_r() 时,响应中只显示 Array()。 - Kshitiz Ghimire
我不懂 PHP。你的意思是它只显示一个空数组吗?那么,首先检查你接收到的数据,然后再检查它是如何解析的。 - Max
我在服务器端只收到了空的POST数据,因此它显示为空数组。 - Kshitiz Ghimire
我大约一年前曾遇到过类似的问题。那种方法解决了这个问题。 - Max
1
嘿,Max,你现在使用了表单提交方法,我非常感谢你的帮助和付出的时间,但是我已经通过使用ASIFormDataRequest库解决了。 - Kshitiz Ghimire

2

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

并且使用以下代码进行了发布:

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];

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

- (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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接