2

我在使用 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);

但我得到了回应。请帮帮我..是我的

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

NSString * str = @"0a"; NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str]; [请求setHTTPBody:[消息数据使用编码:NSUTF8StringEncoding]];[请求 addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

这部分代码是正确的??

4

2 回答 2

1

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

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

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

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

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

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

于 2011-09-21T06:40:23.890 回答
0

你所做的很完美,请在服务器端检查是否通过发送相同的请求来正确响应。

于 2011-09-30T07:43:19.537 回答