Iphone NSURLConnection在设备上不工作
我的Iphone NSURLConnection在设备上不工作,iphone,objective-c,ios,ipad,nsurlconnection,Iphone,Objective C,Ios,Ipad,Nsurlconnection,我的NSURLConnection有问题 我正在使用以下方式向服务器发送请求: [[NSURLConnection alloc] initWithRequest:request delegate:self]; 但我收到一个“连接超时”错误 如果我使用wi-fi连接,它就可以正常工作。我的手机上有一个完善的3g网络,可以以相当快的速度访问其他应用程序和网站 我不明白为什么我的请求没有被发送到服务器 请帮忙 我刚才试过: [[NSURLConnection alloc] initWithReq
NSURLConnection
有问题
我正在使用以下方式向服务器发送请求:
[[NSURLConnection alloc] initWithRequest:request delegate:self];
但我收到一个“连接超时”错误
如果我使用wi-fi连接,它就可以正常工作。我的手机上有一个完善的3g网络,可以以相当快的速度访问其他应用程序和网站
我不明白为什么我的请求没有被发送到服务器
请帮忙
我刚才试过:
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES ];
但这也不起作用。使用NSURLConnection有什么具体原因吗 我有类似的问题,并能够利用NSURLRequest参考下面的代码
-(NSString *)sendFile:(NSString*)url File:(NSString*)pFileName Handler:(NSObject*) sender{
NSString *base64Data = [AppUtil compressAnd64EncodedString:pFileName];
NSMutableString* requestURL = [[NSMutableString alloc] init];
[requestURL appendString:url];
NSMutableString* requestBody = [[NSMutableString alloc] init];
[requestBody appendString:@"request="];
[requestBody appendString:@""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];
// assume we are going to have data of 100 kb
NSMutableString *pRequestBody = [[NSMutableString alloc]initWithCapacity:100*1024];
// append
[pRequestBody appendString:SOAP_START_PACKET];
[pRequestBody appendString:base64Data];
[pRequestBody appendString:SOAP_END_PACKET];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/soap+xml" forHTTPHeaderField:@"content-type"];
[request setValue:@"utf-8" forHTTPHeaderField:@"charset"];
NSData *requestData = [NSData dataWithBytes: [pRequestBody UTF8String] length: [pRequestBody length]];
[request setHTTPBody: requestData];
[request setTimeoutInterval:HTTP_TIME_OUT];
NSError *error = nil;
NSURLResponse *response = nil;
debugLog<<" sending request "<<endl;
NSData *pData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(pData){
NSString *pRespString = [[[NSString alloc] initWithData:pData
encoding:NSUTF8StringEncoding] autorelease];
debugLog<<"Response of length = "<<(int)[pRespString length]<<endl;
// write file depending upon the response
NSString *pAppFileName = [self parseAndGenerateFile:pRespString];
// done with the data
// [pData release];
if(!pAppFileName){
[self setErrorString:@"Server Error"];
return nil;
}
return pAppFileName;
}else{
debugLog<<" data is NULL"<<endl;
NSString *pErrorMessage = [error localizedDescription];
[self setErrorString:pErrorMessage];
/* Must be the Network error, lets show it to the user */
}
return nil;
}
-(NSString*)发送文件:(NSString*)url文件:(NSString*)pFileName处理程序:(NSObject*)发送程序{
NSString*base64Data=[AppUtil compressAnd64EncodedString:pFileName];
NSMutableString*requestURL=[[NSMutableString alloc]init];
[requestURL appendString:url];
NSMutableString*requestBody=[[NSMutableString alloc]init];
[requestBody appendString:@“request=“”;
[requestBody appendString:@”“;
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString STRING stringWithString:requestURL]];
//假设我们将有100KB的数据
NSMutableString*pRequestBody=[[NSMutableString alloc]initWithCapacity:100*1024];
//附加
[pRequestBody appendString:SOAP_START_数据包];
[pRequestBody appendString:base64Data];
[前传正文附录字符串:SOAP_END_数据包];
[请求设置HttpMethod:@“POST”];
[请求设置值:@“应用程序/soap+xml”用于HttpHeaderField:@“内容类型”];
[请求设置值:@“utf-8”用于HttpHeaderField:@“charset”];
NSData*requestData=[NSData dataWithBytes:[pRequestBody UTF8String]长度:[pRequestBody长度]];
[请求setHTTPBody:requestData];
[请求setTimeoutInterval:HTTP_TIME_OUT];
n错误*错误=nil;
NSURLResponse*response=nil;
debugLog使用NSURLConnection有什么具体原因吗
我有类似的问题,并能够利用NSURLRequest参考下面的代码
-(NSString *)sendFile:(NSString*)url File:(NSString*)pFileName Handler:(NSObject*) sender{
NSString *base64Data = [AppUtil compressAnd64EncodedString:pFileName];
NSMutableString* requestURL = [[NSMutableString alloc] init];
[requestURL appendString:url];
NSMutableString* requestBody = [[NSMutableString alloc] init];
[requestBody appendString:@"request="];
[requestBody appendString:@""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];
// assume we are going to have data of 100 kb
NSMutableString *pRequestBody = [[NSMutableString alloc]initWithCapacity:100*1024];
// append
[pRequestBody appendString:SOAP_START_PACKET];
[pRequestBody appendString:base64Data];
[pRequestBody appendString:SOAP_END_PACKET];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/soap+xml" forHTTPHeaderField:@"content-type"];
[request setValue:@"utf-8" forHTTPHeaderField:@"charset"];
NSData *requestData = [NSData dataWithBytes: [pRequestBody UTF8String] length: [pRequestBody length]];
[request setHTTPBody: requestData];
[request setTimeoutInterval:HTTP_TIME_OUT];
NSError *error = nil;
NSURLResponse *response = nil;
debugLog<<" sending request "<<endl;
NSData *pData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(pData){
NSString *pRespString = [[[NSString alloc] initWithData:pData
encoding:NSUTF8StringEncoding] autorelease];
debugLog<<"Response of length = "<<(int)[pRespString length]<<endl;
// write file depending upon the response
NSString *pAppFileName = [self parseAndGenerateFile:pRespString];
// done with the data
// [pData release];
if(!pAppFileName){
[self setErrorString:@"Server Error"];
return nil;
}
return pAppFileName;
}else{
debugLog<<" data is NULL"<<endl;
NSString *pErrorMessage = [error localizedDescription];
[self setErrorString:pErrorMessage];
/* Must be the Network error, lets show it to the user */
}
return nil;
}
-(NSString*)发送文件:(NSString*)url文件:(NSString*)pFileName处理程序:(NSObject*)发送程序{
NSString*base64Data=[AppUtil compressAnd64EncodedString:pFileName];
NSMutableString*requestURL=[[NSMutableString alloc]init];
[requestURL appendString:url];
NSMutableString*requestBody=[[NSMutableString alloc]init];
[requestBody appendString:@“request=“”;
[requestBody appendString:@”“;
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString STRING stringWithString:requestURL]];
//假设我们将有100KB的数据
NSMutableString*pRequestBody=[[NSMutableString alloc]initWithCapacity:100*1024];
//附加
[pRequestBody appendString:SOAP_START_数据包];
[pRequestBody appendString:base64Data];
[前传正文附录字符串:SOAP_END_数据包];
[请求设置HttpMethod:@“POST”];
[请求设置值:@“应用程序/soap+xml”用于HttpHeaderField:@“内容类型”];
[请求设置值:@“utf-8”用于HttpHeaderField:@“charset”];
NSData*requestData=[NSData dataWithBytes:[pRequestBody UTF8String]长度:[pRequestBody长度]];
[请求setHTTPBody:requestData];
[请求setTimeoutInterval:HTTP_TIME_OUT];
n错误*错误=nil;
NSURLResponse*response=nil;
调试日志