xcode NSURLConnection post返回200但NSData始终为null? [英] xcode NSURLConnection post return 200 but NSData always null?

查看:167
本文介绍了xcode NSURLConnection post返回200但NSData始终为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSString *post = @"maxor=123&minor=123";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xavier-beacon.appspot.com/result4.jsp"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]  ;
[request setHTTPMethod:@"POST"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11" forHTTPHeaderField:@"User-Agent"];


[request setValue:[NSString stringWithFormat:@"%d",[postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];


NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connect start];

===================== ================================================== ============================

===================================================================================================

- (void)connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)aResponse
{   
    NSLog(@"state------%d", status);

}
-(void) connection:(NSURLConnection *)connection didReceiveData: (NSData *) incomingData
{    
    NSLog(@"data---%@",[[NSString alloc] initWithData: incomingData encoding:NSUTF8StringEncoding]);

}

以下日志始终为:
(州------- 200)
(数据--- null)

the following log is always : (state-------200) (data---null)

这很容易使用 http://xavier-beacon.appspot.com/result4.jsp?maxor=123&minor=123 获取chrome / IE / Firefox上的响应数据

It is easy to use http://xavier-beacon.appspot.com/result4.jsp?maxor=123&minor=123 to get the response data on the chrome/IE/Firefox

但在iphone中使用xcode总是返回state = 200和data = null QAQ ...

but use xcode in iphone always return state=200 and data=null QAQ...

谁能告诉我如何解决这个问题...... Plz T_T

Who can tell me how can i fix this problem...Plz T_T

推荐答案

状态代码200显示您现在已经与服务器建立了活动且成功的连接。但这并不意味着您将成功获得数据。有一些需要注意的事项:

Status code 200 shows that you have now active and successful connection with the server. But that does not mean that you will get data successfully. There are some points needs to be take care:


  1. 您的请求应符合 Content-Type 。如果不匹配,您将无法接收数据或无效数据

  1. Your request should match Content-Type required on server. If it is not match you will not receive data or invalid data

确保您调用的方法 GET POST

某些网络服务也需要具体 user-agent ,所以如果它还需要检查用户代理

Some web-services are also required specific user-agent, So if it required check for user-agent also

参数值:即使你得到成功代码 - 服务器所需的数据类型值会导致您错误。最常见的可能数据类型不匹配是 bool int float 。必须检查是否已传递确切的数据类型值。例如1被视为 int 以及 bool 值。

Parameter values: Even if you get success code - The datatype values required to server some time mis lead to you. Most common possible datatype mis match are bool, int and float. It is essential to check that you have pass exact datatype value. e.g. 1 is consider as int and also as a bool value.

编码方案:您必须使用与服务器规范完全相同的编码方案来解析接收数据。

Encoding Scheme: You must have to parse receive data with exactly same encoding scheme as per server specification.

修改

在您的情况下,您只需要更改编码:NSASCIIStringEncoding

In your case you just need to change encoding:NSASCIIStringEncoding.

-(void) connection:(NSURLConnection *)connection didReceiveData: (NSData *) incomingData
{
    NSLog(@"data---%@",[[NSString alloc] initWithData: incomingData encoding:NSASCIIStringEncoding]);

}

将上述方法替换为您的代码。

Replace above method with your code.

这篇关于xcode NSURLConnection post返回200但NSData始终为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆