iphone - 使用Post方法+iphone解析xml

标签 iphone nsxmlparser nsmutableurlrequest

我在使用 post 方法进行 xmlparsing 时遇到问题。

API URL :  http://XXX.XXX.X.XX/api/user.php
Function Name : getUserList
Sample XML :
<root>
    <data>
        <id>0</id>
        <search></search>
    </data>
</root>

现在我正在使用:-

// setting up the URL to post to
NSString *urlString = @"http://XXX.XXX.X.XX/api/user.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];        

但是我应该如何在其中制作帖子 HTMLbody 部分..

意味着我应该在代码中的何处以及如何放置函数名称和示例 xml

我编辑的问题是:-

  NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList";

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString * str = @"<root><data><id>0</id><search>a</search></data></root>";
    NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str];
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];





    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"Responce==>%@",returnString);

但是我的 react 变黑了。请帮帮我.. 是我的

 NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList";

NSString * str = @"0a"; NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str]; [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

这部分代码是否正确?

最佳答案

您可以使用方法 - (void)setHTTPBody:(NSData *)data 设置 html body。例如:

[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];

在您的情况下,消息是 xml。

您还需要添加此代码来帮助服务器确定发送数据的类型:

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

如果你想为你的请求设置一些额外的标志,你可以使用方法- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

关于iphone - 使用Post方法+iphone解析xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7495441/

相关文章:

iphone - NSXMLParser initWithContentsOfURL 超时

iphone - iOS教程中的xml解析

iphone - 解析 xml 时 NSCFString appendString 泄漏

ios - Monotouch 中的 objective-C mutableCopy 是如何实现的?

iphone - NSMutableURLRequest 在正文中添加额外信息,导致 Content-Length 不正确

iphone - 要使用 Titanium Appcelerator 或 PhoneGap 需要哪些技能?

iphone - UIView 转换导致崩溃

ios - 使用 NSMutableURLRequest 将海关对象发布到 Web

html - 未安装应用程序时,我的应用程序不显示智能应用程序横幅

ios - iPhone X Swift 底部有空白