在Objective-C iPad开发中进行POST [英] POST in Objective-C iPad dev
问题描述
我正在尝试发出POST请求,而我似乎无法弄清楚发生了什么问题。我收到服务器的回应,但我的电子邮件/密码对似乎没有被正确发送/读取(由服务器),它告诉我没有这样的帐户存在。
这里是我的代码,它包含在一个函数中(当用户按下我创建的登录按钮时调用它)。注意:变量'email'和'password'被设置为我创建的2个文本字段中的值。
NSURL * url = [NSURL URLWithString:@http://mydomain.com/login-ipad/];
NSMutableURLRequest * theRequest = [NSMutableURLRequest requestWithURL:url];
NSString * messageBody = [NSString stringWithFormat:@email =%@& user_pass =%@,email,password];
NSString * msgLength = [NSString stringWithFormat:@%d,[messageBody length]];
[theRequest addValue:@text / xml; charset = utf-8forHTTPHeaderField:@Content-Type];
[theRequest setHTTPMethod:@POST];
[theRequest addValue:msgLength forHTTPHeaderField:@Content-Length];
[theRequest setValue:@application / x-www-form-urlencodedforHTTPHeaderField:@Current-Type];
[theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
NSLog(@连接成功);
parent.loginButton.enabled = NO;
receivedData = [[NSMutableData data] retain];
}
else
{
UIAlertView * alert1 = [[[UIAlertView alloc] initWithTitle:@警报消息:@发送数据时出现问题,请检查您的网络连接。 delegate:self cancelButtonTitle:@OKotherButtonTitles:nil] autorelease];
[alert1 show];
$ / code>
任何人都可以看到我的逻辑有什么问题吗?
$ b $你正在添加一个 Current-Type
头(这AFAIK甚至不是一个有效的头)而不是 Content-Type
。 I'm trying to make a POST request, and I can't seem to figure out what is going wrong. I am getting a response back from the server, but my email/password pair doesn't seem to be being sent/read properly (by the server) and it tells me that no such account exists.
Here's my code that is contained within a function (which is called when the user presses a "login" button I have created). Note: the variables 'email' and 'password' are set to the values in 2 textfields I have also created.
NSURL *url = [NSURL URLWithString:@"http://mydomain.com/login-ipad/"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *messageBody = [NSString stringWithFormat:@"email=%@&user_pass=%@",email,password];
NSString *msgLength = [NSString stringWithFormat:@"%d", [messageBody length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
NSLog(@"Connection Successful");
parent.loginButton.enabled = NO;
receivedData = [[NSMutableData data] retain];
}
else
{
UIAlertView *alert1 = [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"There was an issue sending the data. Please check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert1 show];
}
Can anyone see anything wrong with my logic?
You're adding a Current-Type
header (which AFAIK is not even a valid header) instead of Content-Type
.
这篇关于在Objective-C iPad开发中进行POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!