2011-10-31 51 views
3

发送请求,我想XML文件发送到http://api.online-convert.com/queue-insert如何使用XML

我用这样的代码:

NSString *urlString = [NSString stringWithFormat:@"http://api.online-convert.com/queue-insert"]; 
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:@"<queue>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<apiKey>32423sda..2134</apiKey>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<targetType>audio</targetType>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<targetMethod>convert-to-flac</targetMethod>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<testMode>true</testMode>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<sourceUrl>http://www.online-convert.com/audio/audio-converter.flac</sourceUrl>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"</queue>"] 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); 

    } 

但我总是得到错误:

<queue-answer> 
    <status> 
    <code>8</code> 
    <message>The XML file is empty</message> 
    </status> 
</queue-answer> 

在哪里我的错?请帮助..

+0

退房AFNetworking。它有一些使XML和JSON请求变得微不足道的类。 –

+0

你有这个答案吗? – 2013-10-04 18:06:28

+0

你找到答案了吗? – arniotaki

回答

1

我送XML文件以下列方式:

NSString *message = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" ?>\n<parameters></parameters>"]; 

    url = [NSURL URLWithString:@"https://site.ru/request"]; 
    request = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d",[message length]]; 

    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:msgLength       forHTTPHeaderField:@"Content-Length"]; 

    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; 

    LOG([NSString stringWithFormat:@"Post message: %@"], message); 

    [message release]; 

    self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
+0

返回相同的错误 – LightNight

+0

您是否更改内容类型并设置其长度? – Nekto

+0

当然,但它不工作.. – LightNight

0

你可能不知道你在做什么。在字符串中:

[postBody appendData:[[NSString stringWithFormat:@"<sourceUrl>http://www.online-convert.com/audio/audio-converter.flac</sourceUrl>"] dataUsingEncoding:NSUTF8StringEncoding]]; 

您应该将URL发送到原始文件(您想要转换的文件)所在的服务器。代码中的link会导致不存在的文件。

+0

没关系。它写着'XML文件是空的',所以如果我删除或替换这个参数,它不会改变任何东西 – LightNight

0

这段代码工作对我来说:

- (IBAction)startSOAP:(id)sender 

{ 
     NSLog(@"\n{AppDelegate} startSOAP start"); 

     // create the request 
     NSError **myError; 
     NSHTTPURLResponse **serverResponse; 
     NSData *serverData; 
     NSDictionary *headerFieldsDict = [NSDictionary 
             dictionaryWithObjectsAndKeys:@"Apple iPhone",@"User- Agent", 
             @"text/xml; charset=utf-8", @"Content-Type", 
             @"soapAction",@"SOAP_ACTION",nil]; 
     @try { 

     // 1) The Request String. 
     // Note: smsXMLString contains the entire SMS SOAP envelope, without the <? XML declaration command >. 
     NSString *smsXMLPath = [[NSBundle mainBundle] pathForResource:@"sms" ofType:@"xml"]; 
     self.smsXMLString = [NSString stringWithContentsOfFile:smsXMLPath encoding:NSUTF8StringEncoding error:myError]; 

     // ----------------------- 
     // 2) Create the request. 
     NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:theServerURL] 
                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval:10.0]; 

     // ----------------------- 
     // 2a) Modify the Request from default 'GET' to 'POST': 
     [theRequest setHTTPMethod:@"POST"]; 

     // 2b) Modify the Headers: 
     [theRequest setAllHTTPHeaderFields:headerFieldsDict]; 

     // 2c) Sent the Contents of the Body to the SOAP/XML data: 
     [theRequest setHTTPBody:[self.smsXMLString dataUsingEncoding:NSUTF8StringEncoding]]; 
     // ----------------------- 
     // 3) Get Synchronous Data: 
     serverData = [NSURLConnection sendSynchronousRequest:theRequest 
              returningResponse:serverResponse 
                 error:myError]; 

     // ----------------------- 
     // 4) Convert Synchronous Data into Human-Readable String (Unicode 8) format: 
     NSString *serverDataString = [[[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding] retain]; 

     [[soapResponse layoutManager]replaceTextStorage:[[NSTextStorage alloc] initWithString:serverDataString]]; 

     [serverDataString release]; 

    } 
    @catch (id e) { 
     NSLog(@"\n**** {startSOAP} EXCEPTION: %@ ****\n",e); 
     self.statusLine.stringValue = [NSString stringWithFormat:@"*** Exception flagged: %@ ***",e]; 
       } 
    @finally { 

     NSLog(@"\n{startSoap} end."); 
    } 
}