【问题标题】:POST XML request to 'REST' service url from iPhone从 iPhone 向“REST”服务 URL 的 POST XML 请求
【发布时间】:2012-10-15 09:40:33
【问题描述】:

我对 Objective-C 非常陌生,并且在某一点上感到震惊。我必须发布 XML 请求以休息服务 url 并从中获取响应。我正在使用 NSMutableRequest。但看不到任何响应。不能'不明白我哪里错了……

请求 XML 是这样的:

<GetUserRequest xmlns="http://schemas.datacontract.org/2004/07/DModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ID>456789</ID>
<UserEmail>John@yahoo.com</UserEmail>
</GetUserRequest>

My url is like this : http://192.158.0.104:8732/IServices/GetXml

我的代码是这样的:

NSString *urlString1=[NSString stringWithFormat:@"http://192.158.0.104:8732/IServices/GetXml"];
NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:urlString1]];
[request setHTTPMethod:@"POST"];
NSString *contentType=[NSString stringWithFormat:@"application/xml"];
[request addValue:contentType  forHTTPHeaderField:@"Content-type"];

NSMutableData *postBody=[NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<GetUserRequest xmlns=\"http://schemas.datacontract.org/2004/07/DModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<ID>456789</ID>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<UserEmail>John@yahoo.com</UserEmail>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"/GetUserRequest>"]dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSHTTPURLResponse *urlResponse=nil;
NSError *error=[[NSError alloc]init];
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *rss=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding ];
NSlog(@"Response Code:%d",[urlResponse statusCode]);
if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
{
    NSLog(@"Response:%@",rss);
}

但我在活动控制台中看不到任何内容...

任何帮助将不胜感激......

【问题讨论】:

标签: xcode4 http-post


【解决方案1】:

该代码对我来说看起来很糟糕,并且没有包含足够的错误检查([NSURLConnection sendSynchronousRequest] 可以在发生错误时返回 nil)。将结尾修改为:

NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                                             returningResponse:&urlResponse 
                                                         error:&error];
if (responseData != nil)
{
    NSString *rss = [[NSString alloc] initWithData:responseData 
                                          encoding:NSUTF8StringEncoding ];
    NSlog(@"Response Code:%d",[urlResponse statusCode]);
    if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
    {
        NSLog(@"Response:%@",rss);
    } 
}
else
{
    NSLog(@"Failed to send request: %@", [error localizedDescription]);
}

【讨论】:

  • 我尝试了你的代码。如果我在这一行之后放置一个断点并检查 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returnedResponse:&urlResponse error:&error];之后断点没有进入代码并且控制台区域什么都不显示..甚至 rss 值都没有(可能是 null 或其他东西)我该怎么做?
  • 首先断点没有进入这一行 if (responseData != nil) 来检查它是否为nill或者包含一些数据。Y断点没有停在那一行?
  • OK 看起来像 responseData == nil。将断点放在[NSURLConnection sendSynchronousRequest:...] 行并使用F6 单步执行。
  • 如果 responseData==nil 那么它应该显示错误。我尝试用 F6 检查它并停在 0x003fc7e8 行 jmp *0x52673c 并且它没有向前移动.. donno y 在控制台中,我可以看到单步执行,直到退出函数 dyld_stub_GSEventRun,该函数没有行号信息。
  • 好的,停止在调试器中运行它,直接在没有断点的情况下运行它,看看控制台上会出现什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多