NSURLConnection 返回 0 字节数据
我正在使用同步方法发送 nsurlconection 请求,如下所示。 它在模拟器上工作正常,但在设备上不起作用。
这是我的代码
/////// FOR POST
NSString *postData=[NSString stringWithFormat:@"message=%@",
[[dicGlobal valueForKey:@"giftMessage"] stringByURLEncode]];
NSData *myRequestData = [ NSData dataWithBytes: [ postData UTF8String ] length: [ postData length]];
NSLog(@"%@",myRequestData);
NSURL *url=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];
if (returnData) {
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"%@",content);
TBXML *tbxml=[TBXML tbxmlWithXMLString:content];
请检查我的代码并帮助我找出问题所在。
谢谢&问候 希瓦姆
I am sending nsurlconection request with synchronous method as below.
its working fine with simulator but not working on devices.
here is my code
/////// FOR POST
NSString *postData=[NSString stringWithFormat:@"message=%@",
[[dicGlobal valueForKey:@"giftMessage"] stringByURLEncode]];
NSData *myRequestData = [ NSData dataWithBytes: [ postData UTF8String ] length: [ postData length]];
NSLog(@"%@",myRequestData);
NSURL *url=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];
if (returnData) {
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"%@",content);
TBXML *tbxml=[TBXML tbxmlWithXMLString:content];
Please check my code and help me out where is the issue.
Thanks & Regards
Shivam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这一行中:
什么是 strUrl。它在哪里声明和赋值?
我不确定,但看起来您的 URL 可以从您的 Mac 访问(也许它是您 LAN 上的资源),但不能从您的设备访问。 (您的设备通过其他 Wi-Fi 网络或 3G 连接?)。
尝试从 Mac 创建 Wi-Fi 网络并将您的设备连接到该网络。
希望这能解决问题。
In this line:
what is strUrl. Where is it declared and assigned?
I'm not sure but looks like your URL is accessible from your Mac (maybe it's a resource on your LAN) but not from your device. (Your device connects through some other Wi-Fi network or 3G?).
Try creating a Wi-Fi network from your Mac and connect your device to this network.
Hope this should solve the problem.
实现此代码
Implement this code