NSURLConnection 返回 0 字节数据

发布于 2024-12-27 15:05:08 字数 1132 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2025-01-03 15:05:08

在这一行中:

NSURL *url=[NSURL URLWithString:strUrl];

什么是 strUrl。它在哪里声明和赋值?

我不确定,但看起来您的 URL 可以从您的 Mac 访问(也许它是您 LAN 上的资源),但不能从您的设备访问。 (您的设备通过其他 Wi-Fi 网络或 3G 连接?)。

尝试从 Mac 创建 Wi-Fi 网络并将您的设备连接到该网络。
希望这能解决问题。

In this line:

NSURL *url=[NSURL URLWithString:strUrl];

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.

王权女流氓 2025-01-03 15:05:08

实现此代码

 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 *postRequest = [NSMutableURLRequest requestWithURL:url
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:30.0];
        [ 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];

Implement this code

 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 *postRequest = [NSMutableURLRequest requestWithURL:url
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:30.0];
        [ 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文