gpt4 book ai didi

php - NSURLConnection 连接到服务器,但不发布数据

转载 作者:行者123 更新时间:2023-11-29 10:55:39 25 4
gpt4 key购买 nike

每当我尝试向我的 PHP 服务器发布内容时,我都会收到以下消息。看起来好像代码正在连接到服务器,但是没有返回数据,并且发布数据没有通过。它通过我制作的 Java 应用程序运行,因此我可以保证它们对我的 PHP 没有任何问题。如果您可以帮助我,或者需要更多代码来帮助我,请提出要求。谢谢。

这是为 NSURLConnection 准备变量的代码:

NSString *phash = [NSString stringWithFormat:@"%d",phashnum];
[phash stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
name = _nameField.text;
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
email = _emailField.text;
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

这是我的 NSURLConnection 的代码:

NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
NSOperationQueue *queue= [[NSOperationQueue alloc]init];
NSString *postData = [[NSString alloc] initWithString:stringdata];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if ([data length] > 0 && connectionError==nil){
NSLog(@"Connection Success. Data Returned");
NSLog(@"Data = %@",data);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
else if([data length] == 0 && connectionError == nil){
NSLog(@"Connection Success. No Data returned.");
NSLog(@"Connection Success. Data Returned");
NSLog(@"Data = %@",data);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
else if(connectionError != nil && connectionError.code == NSURLErrorTimedOut){
NSLog(@"Connection Failed. Timed Out");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);

}
else if(connectionError != nil)
{
NSLog(@"%@",connectionError);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);

}
}];

提前致谢。

最佳答案

正如@elk 所说,您应该将空格替换为 + .但是您应该对保留字符进行百分比编码(如 RFC2396 中所定义)。

不幸的是,标准stringByAddingPercentEscapesUsingEncoding不会对所有保留字符进行百分比转义。例如,如果名称是“Bill & Melinda Gates”或“Bill + Melinda Gates”,stringByAddingPercentEscapesUsingEncoding不会逃脱 &+ (因此 + 会被解释为空格,而 & 会被解释为分隔下一个 POST 参数)。

相反,使用 CFURLCreateStringByAddingPercentEscapes , 在 legalURLCharactersToBeEscaped 中提供必要的保留字符参数,然后将空格替换为 + .例如,您可以定义 NSString类别:

@implementation NSString (PercentEscape)

- (NSString *)stringForPostParameterValue:(NSStringEncoding)encoding
{
NSString *string = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
(CFStringRef)@" ",
(CFStringRef)@";/?:@&=+$,",
CFStringConvertNSStringEncodingToEncoding(encoding)));
return [string stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

@end

请注意,我主要关注 &+ , 字符,但是 RFC2396 (取代 RFC1738)将这些附加字符列为保留字符,因此将所有这些保留字符包含在 legalURLCharactersToBeEscaped 中可能是谨慎的做法。 .

将这些放在一起,我可能有将请求发布为的代码:

NSDictionary *params = @{@"name" : _nameField.text ?: @"",
@"email": _emailField.text ?: @"",
@"phash": [NSString stringWithFormat:@"%d",phashnum]};

NSURL *url = [NSURL URLWithString:kBaseURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error)
NSLog(@"sendAsynchronousRequest error = %@", error);

if (data) {
// do whatever you want with the data
}
}];

使用实用方法:

- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
NSMutableArray *paramArray = [NSMutableArray array];
[paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
NSString *param = [NSString stringWithFormat:@"%@=%@", key, [obj stringForPostParameterValue:NSUTF8StringEncoding]];
[paramArray addObject:param];
}];

NSString *string = [paramArray componentsJoinedByString:@"&"];

return [string dataUsingEncoding:NSUTF8StringEncoding];
}

关于php - NSURLConnection 连接到服务器,但不发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18541975/

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