Php 网络连接已断开,无法连接
每次尝试连接时都会打印此错误。不知道为什么它无法连接到服务器。构建了一个简单的java应用程序,运行良好。我已经上网好几天了,想弄明白这一点。尝试了多个API和一些不正确的东西。如果你需要更多的代码,或有任何进一步的问题,让我知道。请帮忙( 2013-08-29 21:56:55.946死机[15054:70b]错误域=NSURLErrorDomain 代码=-1005“网络连接丢失。”用户信息=0xa779a30 {NSErrorFailingURLStringKey=Php 网络连接已断开,无法连接,php,ios,objective-c,nsurlconnection,nsurlrequest,Php,Ios,Objective C,Nsurlconnection,Nsurlrequest,每次尝试连接时都会打印此错误。不知道为什么它无法连接到服务器。构建了一个简单的java应用程序,运行良好。我已经上网好几天了,想弄明白这一点。尝试了多个API和一些不正确的东西。如果你需要更多的代码,或有任何进一步的问题,让我知道。请帮忙( 2013-08-29 21:56:55.946死机[15054:70b]错误域=NSURLErrorDomain 代码=-1005“网络连接丢失。”用户信息=0xa779a30 {NSErrorFailingURLStringKey=http://54.22
http://54.221.224.251/
,
NSErrorFailingURLKey=http://54.221.224.251/
,
NSLocalizedDescription=网络连接已断开。,
NSUnderlyingError=0xa611130“网络连接已丢失。”}
以下是设置请求的代码:
NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
[urlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
NSString *postData = [[NSString alloc] initWithString:stringdata];
[postData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
[connection start];
下面是我的代码,它应该处理来自php服务器的响应
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
}
/*
if there is an error occured, this method will be called by connection
*/
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@" , error);
}
/*
if data is successfully received, this method will be called by connection
*/
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"WORKED!");
}
我需要像这样为stringData添加符号,而不是仅仅添加符号
NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
这解决了我的问题。你能成功连接浏览器吗?因为你的stringdata看起来像GET请求字符串,但你想发布…(我只是有点困惑)我可以通过浏览器成功建立连接。但这并不重要,对吧?因为网络连接丢失了,所以不会抛出错误,它会像500错误一样。另一件奇怪的事情是,如果我将域设置为google.com,它会工作,所以我有点困惑。也许你的url编码没有正确完成,请尝试stringByAddIngPercenteScapesusingEncode:NSUTF8StringEncoding对urlpath和postdata进行编码(仅供参考,最好在请求中添加内容长度)仍然提供网络连接丢失,但我没有添加内容长度,不确定要添加到哪里。我还使用stringByAddingPercentEncoding粘贴了上面更新的代码,以确保我做的一切都正确。