在 NSString 中发布两个键值对
posting two key-value pair in NSString
我正尝试在 NSURLCconnection
中使用 POST
在服务器上 post 登录信息。我必须将两个键值对设置为 emailIdSignIn:abc@def.com
和 passwordSignIn:xxxxxxx
。但问题是,我在服务器上只得到 emailIdSignIn
值, emailIdSignIn
字段中附加了 passwordSignIn
的值,而 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");
}
}
来自Wiki:
When a web browser sends a POST request from a web form element, the
default Internet media type is "application/x-www-form-urlencoded".[8]
This is a format for encoding key-value pairs with possibly duplicate
keys. Each key-value pair is separated by an '&' character, and each
key is separated from its value by an '=' character. Keys and values
are both escaped by replacing spaces with the '+' character and then
using URL encoding on all other non-alphanumeric[9] characters.
所以你需要使用 &
字符而不是 ,
:
NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@&passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text]];
我正尝试在 NSURLCconnection
中使用 POST
在服务器上 post 登录信息。我必须将两个键值对设置为 emailIdSignIn:abc@def.com
和 passwordSignIn:xxxxxxx
。但问题是,我在服务器上只得到 emailIdSignIn
值, emailIdSignIn
字段中附加了 passwordSignIn
的值,而 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");
}
}
来自Wiki:
When a web browser sends a POST request from a web form element, the default Internet media type is "application/x-www-form-urlencoded".[8] This is a format for encoding key-value pairs with possibly duplicate keys. Each key-value pair is separated by an '&' character, and each key is separated from its value by an '=' character. Keys and values are both escaped by replacing spaces with the '+' character and then using URL encoding on all other non-alphanumeric[9] characters.
所以你需要使用 &
字符而不是 ,
:
NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@&passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text]];