gpt4 book ai didi

ios - 在NSString中发布两个键值对

转载 作者:行者123 更新时间:2023-12-01 16:29:55 25 4
gpt4 key购买 nike

我正在尝试使用POST中的NSURLCconnection将登录信息发布到服务器上。我必须将两个键值对设置为emailIdSignIn:abc@def.compasswordSignIn:xxxxxxx。但是问题是,我在服务器上仅获得emailIdSignIn值,并在passwordSignIn字段中附加了emailIdSignIn的值,而passwordSignIn为nil。我也尝试过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:

当网络浏览器从网络表单元素发送POST请求时,
默认的Internet媒体类型为“application / x-www-form-urlencoded”。[8]
这是用于编码可能重复的键/值对的格式
键。每个键值对都由一个'&'字符分隔,并且每个
键由一个'='字符与其值分隔。键和值
都可以通过用'+'字符代替空格来转义,然后
在所有其他非字母数字[9]字符上使用URL编码。

因此,您需要使用&字符而不是,:

NSString *loginData = [NSString stringWithFormat:@"emailIdSignIn=%@&passwordSignIn=%@",[self.emailTextField text],[self.passwordTextField text]];

关于ios - 在NSString中发布两个键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com