简体   繁体   English

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

[英]Post XML data to web server from iphone

i want to post xml data in following format: 我想以下列格式发布xml数据:

  <?xml version="1.0" encoding="UTF-8"?>    
                       <data>                                               
                         <email>xyz@domain.com</email>
                                 <password>xyz123</password>                                      
                       </data>

and receive in following format from the webserver 并从网络服务器接收以下格式

<?xml version="1.0" encoding="UTF-8"?>    
            <user>
                          <user_id>12</user_id> 

                 </user>

help is appreciate now i am trying to use NSUrlConnection and NSMutableRequest i can post data on name like form post,but i want to post just xml data. 帮助是欣赏现在我正在尝试使用NSUrlConnection和NSMutableRequest我可以在名称上发布数据,如表格帖子,但我想发布只是xml数据。 i have also tried to used ASIHTTPRequest . 我也尝试过使用ASIHTTPRequest。 any code or link is highly appriciated. 任何代码或链接都是高度评价的。

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

Of course you can also use asynchronous request. 当然你也可以使用异步请求。 Then you have to implement delegate. 然后你必须实现委托。

Edit. 编辑。 you can also try this: 你也可以试试这个:

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

and finally i used library from http://allseeing-i.com/ASIHTTPRequest/ 最后我用http://allseeing-i.com/ASIHTTPRequest/的

and posted from following code: 并从以下代码发布:

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

and used delegate method to receive data or error. 并使用委托方法接收数据或错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM