1

在您阅读本文之前,请记住,我是一名 Objective C 程序员,试图与一个对我的工作一无所知的 C# 程序员帮助调试服务器问题。

我正在向 .net/c# 端发送一个 SOAP 数据包,并且服务器接收到变量的空值。当 url 和变量字符串放入浏览器时,我得到了正确的响应。

帖子注销到“?help=1& email=test@email.net & password=testpassword”,但我不断收到“Result=Error&Details=Object reference not set to an instance of an object”。返回我的返回字符串。

NSString *post = [NSString stringWithFormat:@"?help=1& email=? & password=%@",userEmail, userPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:LOGIN_SERVICE]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//Submit the Post:
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

/NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//Extract Return:
NSString *returnString = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
4

1 回答 1

2

格式字符串中的电子邮件变量没有被填充。而且,为什么你的帖子数据中有空格?我想这会导致你的问题。因为您的数据可以包含空格,所以您需要对它们进行转义。

代替:

NSString *post = [NSString stringWithFormat:@"?help=1& email=? & password=%@",userEmail, userPassword];

尝试:

NSString* escapedUserEmail = [userEmail stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* escapedUserPassword = [userPassword stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSString *post = [NSString stringWithFormat:@"?help=1&email=%@&password=%@",escapedUserEmail, escapedUserPassword];

有关该主题的更多阅读,请参阅:http ://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html

于 2012-11-26T23:44:46.283 回答