【问题标题】:How to send request with XML如何使用 XML 发送请求
【发布时间】:2011-10-31 12:08:03
【问题描述】:

我想发送 XML 文件到http://api.online-convert.com/queue-insert

我正在使用这样的代码:

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>

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

【问题讨论】:

  • 查看 AFNetworking。它具有使 XML 和 JSON 请求变得微不足道的类。
  • 你有答案吗?
  • 你找到答案了吗?
  • 嗨,你解决了吗?

标签: ios objective-c xml cocoa-touch nsurlrequest


【解决方案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];

【讨论】:

  • 您是否更改了内容类型并设置了其长度?
【解决方案2】:

您可能不知道自己在做什么。在字符串中:

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

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

【讨论】:

  • 没关系。它正在写 The XML file is empty 所以如果我删除或替换这个参数它不会改变任何东西
【解决方案3】:

这段代码对我有用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2012-09-12
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多