gpt4 book ai didi

iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection

转载 作者:可可西里 更新时间:2023-11-01 06:17:06 27 4
gpt4 key购买 nike

我正在努力将 POST 数据发送到服务器并接收到正确的响应。我从使用 setHTTPBody 开始,但是当我没有得到对某些 POST 数据的正确服务器响应并且我读到 setHTTPBody 在早期的 iO​​S 版本上有内存泄漏时,我转向了 setHTTPBodyStream。问题是 setHTTPBodyStream 导致错误 - “操作无法完成。连接由对等方重置”。

代码如下:

NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"secret but correct url goes here"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[request setHTTPMethod: @"POST"];
[request setHTTPBodyStream: [NSInputStream inputStreamWithData: [[NSString stringWithFormat:@"username=%@&password=%@&score=%i",[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)ogl_view->username, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)text_field.text, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],ogl_view->score[0]] dataUsingEncoding:NSUnicodeStringEncoding]]];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
if (connection){
received_data = [[NSMutableData data] retain];
ogl_view->section = CURLING_PROCESS_CREDENTIALS;
}else{
ogl_view->section = CURLING_LOGIN_OR_REGISTER;
UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Can't connect to server" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
[connection_alert show];
[connection_alert release];
}

当我尝试使用网络浏览器时,我可以验证服务器正常运行。

有人可以让我走上正确的轨道,正确地使用 HTTP 和 POST 方法发送数据吗?

感谢您的帮助。

最佳答案

尝试使用它(使用您自己的值):

    NSString* post = @"username=myUser&password=myPass&score=20"; //customize this
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:@"http://my.request.com"]; //customize this
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:timeout];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

然后您需要实现 NSURLConnectionDelegate 方法:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)remoteData;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

最后记得释放NSURLConnection。

编辑:没看到你写的 setHTTPBody 泄漏......从未见过这种情况发生。不管怎样,这里有一些应该可以工作的代码……

关于iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632475/

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