Ios 在NSString中过帐两个键值对
我正在尝试使用Ios 在NSString中过帐两个键值对,ios,objective-c,nsstring,nsurlconnection,Ios,Objective C,Nsstring,Nsurlconnection,我正在尝试使用nsurlconnection中的post在服务器上发布登录信息。我必须将两个键值对设置为emailIdSignIn:abc@def.com和密码签名:xxxxxxx。但问题是,我在服务器上只获得了emailIdSignIn值,并且passwordSignIn的值附加在emailIdSignIn字段中,passwordSignIn为零。我也尝试了NSDictionary,但在这种情况下,两个字段都得到了null。我无法更改服务器端代码。这是我的密码 客户端 /* initiati
nsurlconnection
中的post
在服务器上发布登录信息。我必须将两个键值对设置为emailIdSignIn:abc@def.com
和密码签名:xxxxxxx
。但问题是,我在服务器上只获得了emailIdSignIn
值,并且passwordSignIn
的值附加在emailIdSignIn
字段中,passwordSignIn
为零。我也尝试了NSDictionary
,但在这种情况下,两个字段都得到了null
。我无法更改服务器端代码。这是我的密码
客户端
/* initiating request with the url */
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.2:8080/referamaid/app/noauth/serviceconsumer/login"]];
NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@,passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text],nil];
NSLog(@"login data = %@", loginData);
NSData *postData = [loginData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSError *error;
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];
/* specify the request type */
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
/* set the data to be posted on server into body for "POST"*/
[request setHTTPBody:postData];
NSLog(@"posted data = %@", postData);
/* inititate connection between app and server*/
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
if(!connection)
{
// Release the receivedData object.
receivedData = nil;
// Inform the user that the connection failed.
}
}
}
@RequestMapping(value = "noauth/serviceconsumer/login" , method=RequestMethod.POST)
public String noAuthScLogin(HttpServletRequest request) {
String retVal = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
userAgent = request.getHeader("User-Agent");
String emailId = request.getParameter("emailIdSignIn");
String password = request.getParameter("passwordSignIn");
}
}
服务器端代码
/* initiating request with the url */
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.2:8080/referamaid/app/noauth/serviceconsumer/login"]];
NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@,passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text],nil];
NSLog(@"login data = %@", loginData);
NSData *postData = [loginData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSError *error;
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];
/* specify the request type */
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
/* set the data to be posted on server into body for "POST"*/
[request setHTTPBody:postData];
NSLog(@"posted data = %@", postData);
/* inititate connection between app and server*/
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
if(!connection)
{
// Release the receivedData object.
receivedData = nil;
// Inform the user that the connection failed.
}
}
}
@RequestMapping(value = "noauth/serviceconsumer/login" , method=RequestMethod.POST)
public String noAuthScLogin(HttpServletRequest request) {
String retVal = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
userAgent = request.getHeader("User-Agent");
String emailId = request.getParameter("emailIdSignIn");
String password = request.getParameter("passwordSignIn");
}
}
发件人:
当web浏览器从web表单元素发送POST请求时
默认Internet媒体类型为“application/x-www-form-urlencoded”。[8]
这是一种对可能重复的键值对进行编码的格式
钥匙。每个键值对由一个“&”字符分隔,每个
键与其值之间用“=”字符分隔。键和值
通过将空格替换为“+”字符,然后
对所有其他非字母数字[9]字符使用URL编码
因此,您需要使用&
字符,而不是,
:
NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@&passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text]];
NSLog(@“登录数据=%@”,登录数据)代码>打印正确的数据?您可以显示您尝试的字典代码吗?这是登录数据=emailIdSignIn=k@k.com,passwordSignIn=12345678为什么不使用AFNetworking库,在这里您可以使用参数创建NSDictionary并将其传递给.POST方法?我使用NSDictionary保存数据,然后使用NSDictionary->JSON->POST