gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:24:19 24 4
gpt4 key购买 nike

我正在尝试发出发布请求,但超时后出现错误(以 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com