我是iOS开发的新手,我想向我用PHP语言创建的web服务发送一个请求消息。它将接受XML请求,进行处理,然后提供响应XML消息。
然而,我遇到的问题是,当向when服务发送数据时,它是以NSData形式存在的。
正在发送的数据的NSLog为:
<3c3f786d 6c207665 7273696f etc etc ... 743e>然而,PHP脚本期望得到如下的XML消息:
<?xml version="1.0" ?><request-message><tag-1></tag-1><tag-2></tag-2></request-message>所以我的问题是,有没有办法在不转换成数据的情况下发送NSData,或者有没有办法在PHP Server端把XML字符串转换成可读的XML?
提前谢谢。
模糊的
编辑:要包括请求代码:
// Construct the webservice URL
NSURL *url = [NSURL URLWithString:@"http://localhost/web/check_data.php"];
NSString *requestXML = @"<?xml version='1.0'?><request-message><tag-1>VALUE1</tag-1><tag-2>VALUE2</tag-2></request-message>";
NSData *data = [requestXML dataUsingEncoding:NSUTF8StringEncoding];
// Create a request object with that URL
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];发布于 2011-05-16 04:48:44
在HTTP Body上发送XML并在PHP端解析它,您需要将Content-Type设置为application/xml; charset=utf-8
NSString* sXMLToPost = @"<?xml version=\"1.0\" ?><request-message><tag-1></tag-1><tag-2></tag-2></request-message>";
NSData* data = [sXMLToPost dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://myurl.com/RequestHandler.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[sXMLToPost dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (error) {..handle the error}并在服务器上尝试以下PHP代码:
$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
$http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);https://stackoverflow.com/questions/6011033
复制相似问题