我想将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>
我的错在哪里?请帮忙..
答案 0 :(得分: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];
答案 1 :(得分:0)
你可能不知道自己在做什么。在字符串中:
[postBody appendData:[[NSString stringWithFormat:@"<sourceUrl>http://www.online-convert.com/audio/audio-converter.flac</sourceUrl>"] dataUsingEncoding:NSUTF8StringEncoding]];
您应该将URL发送到原始文件(您要转换的文件)所在的服务器。代码中的link会导致不存在的文件。
答案 2 :(得分: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.");
}
}