【发布时间】:2012-09-13 02:35:54
【问题描述】:
我对我们的网络服务的请求遇到了一个奇怪而烦人的问题。
我正在尝试通过 NSURLRequest 和 NSURLConnection 方法通过 JSON(以及其他一些内容)发送 base64 编码的字符串。
下面的代码适用于小型 base64 编码图像,例如 64x64 像素的头像,但它不适用于较大的 b64 字符串,出于某种奇怪的原因,它会返回“错误请求,400”错误。
我正在使用 POST 方法,字符串长度约为 10,000 个字符。
Web 服务适用于除了这些大字符串之外的所有内容,我尝试了 3 种不同的 b64 解析解决方案,但没有一个有效。
不幸的是,我是前端编码员,几乎没有 Web 服务经验,因此不胜感激。
NSData *postData = [JSONRequest dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URLRequest]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:9999.0f];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setHTTPShouldUsePipelining:NO];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:delegateData];
[connection start];
[connection release];
[request release];
NSLog(@"Connection to %@!",URLRequest);
并且响应被捕获在这里:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"didRecieveResponse");
// cast the response to NSHTTPURLResponse so we can look for 404 etc
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"Response:%@ StatusCode: %i",[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]],[httpResponse statusCode]);
if ([httpResponse statusCode] >= 400)
// do error handling here
}
我有什么遗漏吗?
【问题讨论】:
-
嗨奥利弗!正如您在该问题中看到的那样,我遇到了类似的问题:stackoverflow.com/questions/14910142/…您知道如何解决吗?在这种情况下,如果您能给我一些建议,我将不胜感激。谢谢!
标签: objective-c json web-services post nsurlconnection