ios - 无法发出成功的 POST 请求

标签 ios objective-c iphone http-post

我正在尝试发出发布请求,但超时后出现错误(以 HTML 编码)。这是我的代码:

    if([mode isEqualToString:@"online"])
        {
            NSString *URL = [NSString stringWithFormat:@"http://130.1.5.183:9999/visit-reporting/content/visitreportservice/getCoverInfo"];
        
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
            request.HTTPMethod = @"POST";
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        
            NSDictionary *jsonDictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects: @"gds68342jhgdfk279364jkdbgfjksd82347", @"53afs", @"abc@def.com", @"2009-06-15T13:45:30", @"iPad", nil] forKeys:[NSArray arrayWithObjects:@"responseToken",@"userId", @"email", @"timeStamp", @"userAgent", nil]];
            NSString *requestBodyData = [jsonDictionary description];
        
            NSData *bodyData = [requestBodyData dataUsingEncoding:NSUTF8StringEncoding];
            request.HTTPBody = bodyData;
            
            @try {
                NSOperationQueue *q = [NSOperationQueue mainQueue];
                [NSURLConnection sendAsynchronousRequest:request queue:q completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                    
                    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
                    NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&connectionError];
                    
                    if(!parsedData)
                    {
                        NSLog(@"Not able to parse data");
                    }
                    else
                    {
// DO something
                       }
                }];
            }
            @catch (NSException *exception) {
                NSLog(@"%@", [exception description]);
            }
            @finally {
            }
        }
        else
        {
            //get cover information from offline database
        }

但是,当使用 REST 客户端(名为 COCOA REST CLIENT)检查响应时,会显示所需的响应。问题出在哪里?

这是我从代码发送 POST 请求时得到的响应:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Error</title>
</head>

<body>
    <p>An error has occured</p>
    
    <p>Click <a href="javascript:history.go(-1)">here</a> to navigate back to the page you were on previously.</p>
    
    
        <p>If this problem persists, please contact <a href="mailto:Support.vwg@vwg.co.uk?Subject=Visit Reporting Error&Body=Hi,%0D%0A%0D%0AI have been experiencing problems with the Visit Reporting application.%0D%0A%0D%0ACould you please look into this issue?">Support.vwg@vwg.co.uk</a></p>
    
    
</body>

</html>

最佳答案

这并没有像你预期的那样工作:

NSString *requestBodyData = [jsonDictionary description];

你需要使用NSJSONSerialization的类方法

dataWithJSONObject:options:error:

为了从您的 NSDictionary 对象中获取包含 UTF-8 字符序列的 NSData 对象。

另请参阅:NSJSONSerialization Class Reference

关于ios - 无法发出成功的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27739370/

相关文章:

iphone - 有什么好的iPhone声音管理框架吗?

ios - Xamarin Forms - 当键盘出现且内容位于 ScrollView 内时,iOS 输入字段不会自动调整位置

ios - 是否 openParentApplication :reply require App Groups capability to be enabled?

ios - 如何让我的 Facebook GraphAPI 数据在 UITableView 中不重复?

iphone - 向 UIDatePicker 添加按钮

ios - Touch ID - 如何进入密码屏幕? - swift

iOS 收据验证 d2i_X509_bio 返回 NULL

ios - 遍历给定部分 id 的所有 UITableCells

ios - 将 Apple Watch 添加到具有部署目标 IOS 7 的 iOS 应用程序

objective-c - 访问 DOM 对象模型 JavascriptCore