簡體   English   中英

從iphone將XML數據發布到Web服務器

[英]Post XML data to web server from iphone

我想以下列格式發布xml數據:

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

並從網絡服務器接收以下格式

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

                 </user>

幫助是欣賞現在我正在嘗試使用NSUrlConnection和NSMutableRequest我可以在名稱上發布數據,如表格帖子,但我想發布只是xml數據。 我也嘗試過使用ASIHTTPRequest。 任何代碼或鏈接都是高度評價的。

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

當然你也可以使用異步請求。 然后你必須實現委托。

編輯。 你也可以試試這個:

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

最后我用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM